Results 1 to 7 of 7
  1. #1
    Uma R is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default issue with FileReader and FileWriter

    hi all,
    please help me here

    I am getting a unwanted answered to write and read to file .
    Below is the code of it

    The size of array is coming as -1 and hence if retrieve values from the array also, it is displaying null.

    package chap6;

    import java.io.*;

    public class Chap61 {
    public Chap61() {
    }

    public static void main(String[] args) {
    try {
    // char [] n = new char[50];

    int size = 0;
    boolean newFile = false;

    File file = new File("testFile.doc");
    System.out.println("file exists:" + file.exists());
    file.createNewFile();
    //below is to wrap the file with FileWriter
    BufferedWriter bw= new BufferedWriter(file);
    bw.write("uma");
    // FileWriter nf = new FileWriter(file);
    // nf.write("first");
    System.out.println("new fiel created" + newFile);
    System.out.println("file exists:" + file.exists());
    //below is to wrap the file with FileWriter
    // FileReader fr = new FileReader(file);
    BufferedReader br= new BufferedReader();
    size=br.read(n);
    //size = fr.read(n);
    System.out.println("new file size is :" + size);
    for (char c: n) {
    System.out.print("characters are :" + c + "\n");
    }

    } catch (IOException ex) {
    ex.printStackTrace();
    }


    }
    }

    result of the array is :
    new file size is :-1


    Please let me know where I am going wrong

    Thanks
    Uma

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The "read" method returns -1 at EOF, which will happen immediately when you create a BufferedReader that doesn't wrap anything.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    I don't think your code is compiling.Please try to correct the compilation errors first.
    Ramya:cool:

  4. #4
    Uma R is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks for the reply ,

    my code is compiling fine and is still giving me the size as -1.

    Please help me to solve this

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What do you not understand from reply #1? You are creating a BufferedReader that doesn't wrap anything. I.E. A BufferedReader that isn't reading anything. What is the purpose of that?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Just to help the previous replier; here:

    Java Code:
    BufferedReader br= new BufferedReader(); // <--- this wraps nothing
    size=br.read(n); // <--- so this read returns -1
    kind regards,

    Jos

  7. #7
    rkumar is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    try to use below code.

    package chap6;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    public class Chap61 {
    public Chap61()
    {
    }

    public static void main(String[] args)
    {
    try {
    // char [] n = new char[50];
    int size = 0;
    boolean newFile = false;
    File file = new File("C://testFile.doc");
    System.out.println("file exists:" + file.exists());
    file.createNewFile();
    // below is to wrap the file with FileWriter
    //FileOutputStream fos = new FileOutputStream(file);
    FileWriter fw = new FileWriter(file);
    BufferedWriter bw= new BufferedWriter(fw);
    bw.write("uma");
    bw.flush();
    bw.close();
    // FileWriter nf = new FileWriter(file);
    // nf.write("first");
    System.out.println("new fiel created" + newFile);
    System.out.println("file exists:" + file.exists());
    // below is to wrap the file with FileWriter
    // FileReader fr = new FileReader(file);
    FileReader fr = new FileReader(file);
    BufferedReader br= new BufferedReader(fr);
    size=br.read();
    // size = fr.read(n);
    System.out.println("new file size is :" + size);
    /* for (char c: n)
    {
    System.out.print("characters are :" + c + "\n");
    }*/
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }


    }
    }

Similar Threads

  1. FileWriter doesn't write
    By superman5 in forum NetBeans
    Replies: 2
    Last Post: 07-30-2009, 05:46 AM
  2. FileReader help
    By emp in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:41 AM
  3. JAva Filewriter
    By tommyyyy in forum New To Java
    Replies: 1
    Last Post: 03-28-2009, 12:51 PM
  4. scanner vs. BufferedReader? printstream vs. FileWriter?
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 01-09-2009, 01:58 AM
  5. New line or Carriage Return through FileWriter
    By johnt in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 09:13 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
  •