Results 1 to 3 of 3
  1. #1
    jspexception is offline Member
    Join Date
    Jan 2012
    Posts
    27
    Rep Power
    0

    Default File Writting problem??????

    Code is::::


    public static void getFile(String s) throws IOException
    {

    File f=new File(s);
    boolean b=f.exists();
    if(b)
    {
    System.out.println("File Exist ");
    try
    {
    FileInputStream fstream = new FileInputStream(s);

    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    FileWriter fostream = new FileWriter("d:/report.txt");

    //file is created on d:/report.txt

    BufferedWriter out = new BufferedWriter(fostream);

    while ((strLine = br.readLine()) != null) {

    String line_allow="HELLO";
    out.write(line_allow);

    //but string is not written in file report.txt

    }
    }
    catch(Exception e)
    {

    }
    }
    else
    System.out.println("File Not Exist ");
    }





    Problem is the file report.txt does not contain anything?

    blank file !!!
    what is the problem in code???


    help?????

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: File Writting problem??????

    Quote Originally Posted by jspexception View Post
    Code is::::

    Java Code:
       public static void getFile(String s) throws IOException
         {
             
         File f=new File(s);
         boolean b=f.exists();
         if(b)
         {
             System.out.println("File Exist ");
             try
             {
                  FileInputStream fstream = new FileInputStream(s);
    
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
     FileWriter fostream = new FileWriter("d:/report.txt");
    
    //file is created on d:/report.txt
    
      BufferedWriter out = new BufferedWriter(fostream);
    
        while ((strLine = br.readLine()) != null)   {
    
            String line_allow="HELLO";
              out.write(line_allow);
    
    //but string is not written in file report.txt
             
      }
    }
             catch(Exception e)
             {
    
             }
         }
         else
             System.out.println("File Not Exist ");
     }
    help?????
    First use code tags, otherwise the code is hard to read.
    Next, never eat exceptions...always at the least do e.printStackTrace(), otherwsie you'll never know when something has gone wrong.

    Also, your use of streams and readers is seriously confused. That DataInputStream is completely unecessary. It should be simply:
    Java Code:
          BufferedReader br = new BufferedReader(new FileReader(f));
    Also (and I suspect this could be your real problem) you don't seem to ever close the streams (which should be done in a finally block as part of the try/catch)?

  3. #3
    dheaven is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

Similar Threads

  1. txt reading and writting
    By zenitis in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 01:27 AM
  2. java file writting
    By karq in forum New To Java
    Replies: 3
    Last Post: 07-07-2010, 09:38 PM
  3. Problem with writting in JEditorPane.
    By marox in forum AWT / Swing
    Replies: 11
    Last Post: 04-21-2010, 12:25 AM
  4. Writting output to file!
    By hakan123 in forum New To Java
    Replies: 8
    Last Post: 11-19-2009, 05:39 PM
  5. writting extended ascii chars on socket........or Endianness Issue......??
    By sachinj13 in forum Threads and Synchronization
    Replies: 8
    Last Post: 09-23-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •