Problem with printing to files....................

Well....i need to write a progr tat transfers filename from client to server asnd then the server coies that file(in a directory)....but im not able to do it...im unable to write to files...i dont know y?....
import java.io.*;
import java.net.*;
public class fserver
     public static void main(String args[]) throws Exception
          ServerSocket ss=new ServerSocket(9090);
          Socket s=ss.accept();
          while(true)
               DataInputStream dis=new DataInputStream(s.getInputStream());
               PrintStream ps=new PrintStream(s.getOutputStream());
               String s1=dis.readLine();
               DataInputStream dis1=new DataInputStream(new FileInputStream(s1));
String savepath="";
savepath=s1.substring(1+s1.lastIndexOf('\\'));
System.out.println("Saving file as:"+savepath);
FileWriter fw=new FileWriter(savepath);
               String st;
fw.write("dfjsdklfdlk");//well the file should atleast contain this.......but it is not receiving even this....but the next //system.out.pritln is working perfectly(in the while loop)
               /*while((st=dis1.readLine())!=null)
                    System.out.println(st);fw.write(st);
ps.println("File OK");
               System.out.println("Received the requested file");System.exit(1);
}

Hi,
I would recommend to start reading this http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
Or
There is more efficient way by using FileChannel ex;
public class FileServer {
     static int counter = 1;
     public static void main(String args[]) throws IOException {
          ServerSocketChannel serverSocket = null;
          try{
                serverSocket = ServerSocketChannel.open();
               serverSocket.socket().bind(new InetSocketAddress(444));
               String fName = "c:\\tmp\\abc"+"_"+counter + " .txt";
               FileOutputStream fos = new FileOutputStream(fName);
               FileChannel incoming =fos.getChannel();
               long count = incoming.size();
               while(true){
                    SocketChannel sock = serverSocket.accept();
                    incoming.transferFrom(sock, 0, 1024);
                    counter++;
          }catch(Exception e){
               e.printStackTrace();
          }finally {
               serverSocket.close();
* This is any client tries to send a file into the server
* @author alan
public class FileClient {
     public static void main(String args[]) throws IOException{
          FileChannel fChannel = null;
          FileInputStream inFile = null;
          try {
               SocketChannel clientSocket = SocketChannel.open(new InetSocketAddress("localhost", 444));
                inFile = new FileInputStream("c:\\tmp\\file1.txt");
                fChannel = inFile.getChannel();
                long count = inFile.getChannel().size();
                System.out.println(count);
                fChannel.transferTo(0,count, clientSocket);
          } catch (NumberFormatException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }finally {
            fChannel.close(); // always
            inFile.close(); // always
}Regards,
Alan Mehio
London,UK

Similar Messages

  • Problem with printing pdf file

    hello,
    lately i have problem with printing pdf file:
    i click on the print button and the respod is very slow
    it takes more than a minute till the print window apears.
    i tryied to install version X but it's the same, the problem exist only when printing via adobe reader,
    other programs i'd working much faster.
    please advice,
    thank you

    No, it is not set to overprint. Also, I have to add that the printing results were not consistent. Sometimes it covered and sometimes it did not.

  • Problem with printing txt file

    I have a problem with printing txt file. My code looks like :
    String filename = something.txt";
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(
              flavor, null);
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(PrintQuality.DRAFT);
    pras.add(new Copies(1));
    pras.add(MediaSizeName.ISO_A4);
    pras.add(Sides.ONE_SIDED);
    pras.add(Chromaticity.MONOCHROME);
    PrintService service = ServiceUI.printDialog(null, 200, 200,
              services, services[0],
              null,
              pras);
    if (service != null)
    try
    DocPrintJob job = service.createPrintJob();
    FileInputStream fis = new FileInputStream(filename);
    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(fis, flavor, das);
    job.print(doc, pras);
    Thread.sleep(10000);
    catch(FileNotFoundException e)
    catch(PrintException e1)
    catch(InterruptedException e2)
    It doesn't work :(
    What is my purpose: I would like to print .txt file but I would like to get PrintDialog where I can choose PrintQuality, Chromaticity etc. I found also that I should use DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST but I don't know how.
    I will be very gratefull for help :)

    I tested it on 3 printers (different models of hp). 2 of them don't give me any error but when it started print priter stoped (just as it dosesn't have a paper), but 1 of them doesn't react (there were no errors), i thought tahat something was wrong with printers but when I change DocFlavor.INPUT_STREAM.AUTOSENSE to DocFlavor.GIF and filename to dog.gif it works so mybe there is another way to print txt file

  • Problem with printing pdf file created from Illustrator

    Hello,
    In a drawing I created with Illustrator 10.0.3 I covered part of a circle with a white rectangle (to make it disappear). I saved it as pdf. When printing the pdf (under Linux with a HP printer), the covered part reappeared. However the pdf-file is how it should be. Anybody knows what the problem is?

    No, it is not set to overprint. Also, I have to add that the printing results were not consistent. Sometimes it covered and sometimes it did not.

  • Problem with printing from file to printer

    I've been having problems printing more than one page of text from a file to a specified printer in addition to the fact that the number of pages the print dialog says are to be printed is always 9999! I have tried overriding functions from Pageable too, which gave me problems. I tried making the pageIndex more than one page, but it just prints out blank pages after the first, even if there is more data in the file that should have been printed. Here is my code, and if someone could give me some helpful advice I would very much appreciate it! (Thanks in advance for your advice!)
    import java.awt.*;
    import java.awt.print.*;
    import java.io.*;
    import javax.swing.*;
    public class MyPrinter implements Printable, Runnable { //, Pageable {
    private static Font fnt = new Font("Serif", Font.PLAIN, 11);
    protected PrinterJob job;
    protected MyDialog dialog = new MyDialog();
    protected RandomAccessFile raf;
    protected JFrame mainFrame;
    protected String fileName;
    protected File printFile;
    public MyPrinter(JFrame mFrame) { mainFrame = mFrame; } // end constructor
    public void printFile(String name) {// gives error message if file doesn't exist!
    printFile = new File(name);
    fileName = name;
    if((printFile.exists()) && (printFile.canRead())) {
    Thread myThread = new Thread(this);
    myThread.start();
    } // end if
    else {
    dialog.showError("Datei "+fileName+" existert nicht!", "Fehler mit Drucken");
    } // end else
    } // end printFile
    public void run() {
    job = PrinterJob.getPrinterJob();
    job.setPrintable(this); // Printable is an instance of myPrint
    if(job.printDialog()) { // user didn't cancel the printing
    try { job.print(); } // end try
    catch (PrinterException pe) {dialog.showError("Fehler: "+pe.getMessage(), "Problem");}
    } // end if
    } // end run
    public int print(Graphics g, PageFormat pf, int pageIndex)
    throws PrinterException {
    // pageIndex # corresponds to page number # + 1
    if (pageIndex > 0) { return Printable.NO_SUCH_PAGE; } // end if
    else {
    g.setFont(fnt);
    g.setColor(Color.black);
    int x = 75, y = 80;
    String oneLine;
    try {
    raf = new RandomAccessFile(fileName, "r");
    int fileLength = (int)printFile.length();
    while((((int)raf.getFilePointer()) < fileLength)) {
    oneLine = raf.readLine();
    g.drawString(oneLine, x, y);
    y += 15;
    } // end while loop
    raf.close();
    } // end try
    catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
    return Printable.PAGE_EXISTS;
    } // end else
    } // end print
    /*public Printable getPrintable(int pageIndex) {
    return this;
    } // end getPrintable
    public int getNumberOfPages() {
    return 1;
    } // end getNumberOfPages
    public PageFormat getPageFormat(int pageIndex) {
    return job.defaultPage();
    } // end getPageFormat
    } // end MyPrinter class

    Well, I changed some things around, and assuming that in a file around 45 lines is a normal page, I am able to get the right amount of pages to be displayed in the dialog and to print, but still, the first page is printed fine and the rest are blank. I am baffled as to what I'm doing wrong. Printing to a printer should not be this hard to do! I have also noticed that many other people have had the same problem, but with no solutions. I am using the jdk 1.3.1 - is that a problem? Here is my new code and if anyone can propose a solution to the blank pages problem I would be very grateful!
    import java.awt.*;
    import java.awt.print.*;
    import java.io.*;
    import javax.swing.*;
    public class MyPrinter implements Runnable {
    protected PrinterJob job;
    protected MyPrintable myPrint = new MyPrintable();
    protected MyPageable myPage = new MyPageable();
    protected MyDialog dialog = new MyDialog();
    protected RandomAccessFile raf;
    //protected Startmenu mainMenu;
    protected String fileName;
    protected File printFile;
    protected int numPages = 0;
    public MyPrinter() { } // end constructor
    public void printFile(String name) {// gives error message if file doesn't exist!
    printFile = new File(name);
    fileName = name;
    if((printFile.exists()) && (printFile.canRead())) {
    Thread myThread = new Thread(this);
    myThread.start();
    } // end if
    else {
    dialog.showError("Datei "+fileName+" existert nicht!", "Fehler mit Drucken");
    } // end else
    } // end printFile
    public void run() {
    numPages = findNumPages();
    job = PrinterJob.getPrinterJob();
    job.setPageable(myPage); // this is an instance of Pageable
    if(job.printDialog()) { // user didn't cancel the printing
    try {
    job.print();
    try { raf.close(); } // end try
    catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
    } // end try
    catch (PrinterException pe) {dialog.showError("Fehler: "+pe.getMessage(), "Problem");}
    } // end if
    } // end run
    private int findNumPages() {
    try {
    numPages = 0;
    raf = new RandomAccessFile(fileName, "r");
    int fileLength = (int)printFile.length();
    int lineNumber = 0;
    while((int)raf.getFilePointer() < fileLength) {
    lineNumber = 0;
    while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 45)) {
    raf.readLine();
    lineNumber++;
    } // end while loop
    numPages++;
    } // end while loop
    raf.seek(0);
    } // end try
    catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
    return numPages;
    } // end findNumPages
    public class MyPageable implements Pageable {
    public Printable getPrintable(int pageIndex) { return myPrint; } // end getPrintable
    public PageFormat getPageFormat(int pageIndex) {
    PageFormat pf = new PageFormat();
    pf.setOrientation(PageFormat.PORTRAIT);
    return pf;
    } // end getPageFormat
    public int getNumberOfPages() {
    return numPages; // test
    //return ((int)printFile.length()/4000); // assume 4KB per page
    } // end getNumberOfPages
    } // end MyPageable class
    public class MyPrintable implements Printable {
    private Font fnt = new Font("Serif", Font.PLAIN, 11);
    protected int lastPageIndexChecked = -1;
    public int print(Graphics g, PageFormat pf, int pageIndex)
    throws PrinterException {
    // pageIndex # corresponds to page number # + 1
    pf.setOrientation(PageFormat.PORTRAIT);
    if (pageIndex >= numPages) { return Printable.NO_SUCH_PAGE; } // end if
    else {
    g.setFont(fnt);
    g.setColor(Color.black);
    String oneLine;
    int x = 75, y = 80, lineNumber = 0;
    try {
    int fileLength = (int)printFile.length();
    while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 50)) {
    oneLine = raf.readLine();
    lineNumber++;
    g.drawString(oneLine, x, y);
    y += 15;
    } // end while loop
    } // end try
    catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
    return Printable.PAGE_EXISTS;
    } // end else
    } // end print
    } // end MyPrintable class
    } // end MyPrinter class

  • Problem with printing pdf file HP 845c windows 7

    When I am printing a pdf file it is not printed well, the ink is grey and it is difficult to read.

    No, it is not set to overprint. Also, I have to add that the printing results were not consistent. Sometimes it covered and sometimes it did not.

  • Problem with printing files created by tcpdf class

    Hello,
    I have problem with printing pdf files created with tcpdf php class which contain embedded fonts
    http://www.tecnick.com/pagefiles/tcpdf/example_008.pdf
    Could someone try to print that example file at his Reader (printing to file .ps will be fine).
    I'm trying to determinate if problem is in my system (Ubuntu 8,04) in Reader or in class.
    Thanks in advance

    No problem with Adobe Reader 8 or 9 .

  • Printing Error: There is a problem with a font file.

    I get this error when I try to print:
    "Printing Error: There is a problem with a font file."
    I tried exporting to PDF and printing from there. I get this message:
    "An error exist on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem."
    Has anyone encountered this problem?

    You may have a corrupted font. Try substituting fonts to see which one is the problem.

  • Problem with printing graphics in SAP Script

    Hi,
    I am facing problem with printing of graphics in SAP Script. I uploaded a bitmap file from pc to se78. Then I inserted it in SAP script.
    I am able to view it in the printpreview as well as in the spool number. But it is not getting printed.
    Plese tell me the solution.
    Thanks,
    Srinivas

    You need to check your printer settings.Might be drivers are missing.

  • Problem with printing from Acrobat Reader XI

    SInce I installed Acrobat Reader XI I have got problems with printing out pages. The text starts a bit down (2 cm) on the paper, resulting that the last part of the text on the page will be missing. And the missing part does not follow on page 2, it just don´t comes out!
    It doesn´t matter what settings i click in on the print setting page in Acrobat Reader, it always gets the same result.
    Does anyone know what can be the problem?

    No files appeared in C:\Users\Pavilion\AppData\Roaming\Adobe\Adobe PDF\Settings but they did appear in Distiller.
    I tried to copied them from C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Settings, but found they were already there.  I copied them by adding -COPY before .joboptions in the name.  Now they appeared in  Printing Preferences with the -COPY added.
    I tested a print from Word (using File / Print), but was not prompted for the filename or location.  I checked my entire system and couldn't find the PDF file anywhere.  I tried changing the Port to Desktop\*.pdf using Printer Properties, but that didn't work either.
    I was able to successfully use the Acrobat menu item in Word & Excel to create a PDF but it automatically created the file on the Desktop without an option to change the filename.
    After several days and hours, I gave up and installed Acrobat X on the W7 computer.  I can still use Acrobat XI on the W8 computer.  So be it!
    Thanks anyway for your attempts to help.

  • Problem with printing more than one page (ghostscript broken?)

    Hello,
    since one of the last package updates I have problems with printing more than one page. The print problems occurs as well with pdf files as well with printing websites. I use an actual KDE system with commercial turboprint printer driver. The first page always prints fine, but all consecutive pages are corrupted.  As from a similar problem some months ago, I assume a broken ghostscript package. I tried to downgrade ghostscript to check that, but I get depency problems:
    root@arch32 dieter]# ls -l /var/cache/pacman/pkg/ghostscript-*
    -rw-r--r-- 1 root root  9906288 21. Mär 2010  /var/cache/pacman/pkg/ghostscript-8.71-3-i686.pkg.tar.xz
    -rw-r--r-- 1 root root 11402624  8. Okt 22:44 /var/cache/pacman/pkg/ghostscript-9.00-1-i686.pkg.tar.xz
    [root@arch32 dieter]# pacman -U /var/cache/pacman/pkg/ghostscript-8.71-3-i686.pkg.tar.xz
    Löse Abhängigkeiten auf...
    Suche nach Zwischen-Konflikten...
    Fehler: Konnte den Vorgang (Kann Abhängigkeiten nicht erfüllen) nicht vorbereiten
    :: libspectre: benötigt ghostscript>=9.00
    [root@arch32 dieter]#
    Anyone else having printing problems or a solution for my problem?
    Thanks, Dieter

    This may not be related but I had a problem printing a pdf today whereby the second page got corrupted. The same pdf had printed fine several months ago. I realised that (long story) for some reason I had switched drivers (HP printer) from hpijs to hpcups. I installed the printer again as a "new" printer using the hpijs driver and it printed properly this time.
    So it may be that the bug causes issues with some drivers. You could try an alternative driver for the printer as temporary workaround?

  • Problem with printing some report

    hi very body
    we have some problems with printing some reports.
    the problem is when printing a report the computer processes stoped
    and i should restart the computer a gain to print the report for first time and
    then in the second time i have same problem.

    Maher,
    You can check out the new printer driver test utility verions2.0 on OTN at:
    http://otn.oracle.com/software/products/reports/files/printerdrivertest.zip
    This should help you determine if it is a printer problem or whether you should look into another area.
    Also, you don't mention which version of reports, what command line options your using, the computer
    configuration your using whether it is a network printer or local printer connected to the machine. Support
    will need this information to help determine where your problem lies.
    Regards,
    The Oracle Reports Team

  • Tempo problems with imported wav files

    Hey everyone, sorry if there's a quick fix for this in the forums that I couldn't find, but I've been having some tempo problems with imported .wav files.
    Long story short, my system couldn't handle playing all the tracks for a song while recording drums, so I bounced out an mp3 of the song and put it in a new Logic file so my drummer could just play along to that as I recorded him. Unfortunately, the original song is at 167 bpm, but I forgot to change the bpm in the new Logic file with the .mp3 file of the song to 167 bpm, so it was left at the default 120 bpm.
    So, the drums were recorded at the correct 167 bpm, but Logic thinks that those new drum .wav files should be played at 120 bpm, so when I import my drum tracks back into the original file, they do not play correctly at all.
    I could get record it all again, but I wanted to check if there was a way I could salvage what I already have, since my drummer lives about an hour away right now and can't just come over whenever he wants.
    Thanks for the help! I really appreciate it.

    Hi,
    First, do not use MP3 in Logic, the sound quality is less than AIFF, WAV or CAF, and Logic has to decode it for playback, making it a heavier burden on the CPU than an uncoded audiofile, such as AIFF, WAV or CAF.
    Secondly, audio files are independent of Logic's tempo. If you bounce down an audio file in any format (other than Apple Loop), it will play back at the same speed, +regardless of Logics' tempo setting+, either at recording or playback. Logic doesn't 'think' anything. The BPM is only important to MIDI tracks, or to the spacing between audio files. The audio files themselves *are not affected* by the tempo setting. If you import an audio file of tempo 167 into a 120 BPM project, the file will still play at 167, only Logic will indicate the wrong bar positions.
    regards, Erik.

  • Problem with preview PDF files on Windows 7 32bit

    Hello,
    in my office I have a problem with preview PDF files on my computers. The files are allright, but if I select a file and click on preview mode after ~ 10sec operation system write something "Preview this file is corrupt for PDF Preview Handler wrong". On other 64bit computers with same programs the preview is work. I try uninstall all Adobe soft (Reader, Flash), clean all Adobe data from folders in root and in register, but it is not solve this problem. On Microsoft forums wrote me, that the problem is probably with AR plugin.
    So, can someone help me?
    Config PC:
    Windows 7 32/64bit Pro
    Intel i3, 4GB
    Adobe Reader 11.0.5
    Adobe Flash Player 15 ActiveX
    Adobe Flash Player 15 plugin
    Preview Handler Surrogate Host ver.: 6.1.7601.17514
    Thank you for help.

    I think I figured it out after reading other forums. Thanks!

  • HP Officejet Pro 8600 "Problem with Printer or Ink System'

    A gander at this forum suggests I am one ofa multitude of people perplexed by this issue. There is a notation beside one enquiry which says 'Problem Solved'.... well it isn't for me. My printer (typical!) is one month out of warranty. I had to replace the black cartridge and, when I did, it came up with this error prompt. I then replaced all the other cartridges. HP suggests removing the printhead and following that they say place it 'upside down on a clean sheet of paper'... NO instructions for cleaning it!! When it is replaced in the printer they suggest you 'rock the printhead left and right to make sure that the printhead fully seats in the carriage' and if THIS doesn't fix things 'try reseating the printhead up to 3 times'... This in itself is an admission of an 'issue' with this product. It doesn't sound like a very scientific engineering 'fix' to me!
    A new printhead costs almost what we paid for the printer a year ago. I hate consigning anything to landfill that I can avoid... unfortunately it looks like that is where this unit belongs...

    Hi @Robin_Perth,
    Welcome to the HP Forums!
    I am sorry to hear about you getting the problem with printer or ink system error, with your HP Officejet 8600, but I am happy to look into this for you!
    I am assuming you have already been through this guide, A 'Problem with Ink System' Error Message Displays on the Control Panel or Computer During Setup.
    If this guide does not resolve the issue, please call our technical support at 800-474-6836. If you live outside the US/Canada Region, please click the link below to get the support number for your region. Country-language selector.
    This guide, Limited Warranty for HP Ink Cartridges and Customer-Replaceable Printheads for HP Inkjet Supplies, will help you to determine if your printhead is still in warranty or not.
    Hope this helps!
    RnRMusicMan
    I work on behalf of HP
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos Thumbs Up" to say “Thanks” for helping!

Maybe you are looking for