Reading lines from a text file using The BufferedReader class

Discussion in 'Java' started by Bob, Dec 3, 2004.

  1. Bob

    Bob Guest

    i have to make a program that creates a file called input.txt.
    the user goes into this file and types a list of words in a line on
    many lines
    the program is suppose to make a file called output.txt that takes
    every word from input.txt and displays one per line. words that are
    made out of four letters get replaced to four stars, ****.

    i did the program, the only problem is, it will only read the first
    line of the input.txt file for some reason. Here is my code from the
    reading of input.txt and writing of output.txt if u can tell me why it
    won't read the second line in input.txt that owuld be great!

    public static void readFile ()
    {

    String word = "", line = "";
    BufferedReader input;
    PrintWriter output;

    try
    {
    input = new BufferedReader (new FileReader ("Input.txt"));
    output = new PrintWriter (new FileWriter ("Output.txt"));

    line = input.readLine ();

    while (line != null)
    {
    try
    {
    for (int i = 0 ; i < line.length () ; i++)
    {
    if (line.charAt (i) == ' ')
    {
    if (word.length () == 4)
    output.println ("****");
    else
    {
    output.println (word);
    }
    word = "";
    }
    else
    word += line.charAt (i);
    }
    if (word.length () == 4)
    output.println ("****");
    else
    output.println (word);

    line = input.readLine ();

    output.close ();

    } //end try
    catch (NullPointerException e2)
    {
    }
    } //end while != null
    } //end try
    catch (IOException e)
    {
    }
    }


    thnx a lot guys ;)
     
    Bob, Dec 3, 2004
    #1
    1. Advertising

  2. Bob

    NOBODY Guest

    -stop silently catching exceptions...
    -read the jdk doc



    public static void readFile() {
    BufferedReader input = null;
    PrintWriter output = null;

    try {
    input = new BufferedReader(new FileReader("Input.txt"));
    output = new PrintWriter(new BufferedWriter(new
    FileWriter("Output.txt")));

    String line = null;
    while( (line=input.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(line, "\r\n
    \t\f ");
    while(st.hasMoreTokens()) {
    String word = st.nextToken();
    if(word.length()==4)
    output.println("****");
    else
    output.println(word);
    }
    }

    output.flush();

    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    input.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    output.close();
    }
    }
     
    NOBODY, Dec 3, 2004
    #2
    1. Advertising

  3. Bob

    Alex Kizub Guest

    Bob wrote:

    > i did the program, the only problem is, it will only read the first
    > line of the input.txt file for some reason.


    It reads all lines. You only close output too soon.
    Move this close line outside the while loop. You close it for each line :)

    Alex Kizub.
     
    Alex Kizub, Dec 3, 2004
    #3
  4. Andrew Thompson, Dec 3, 2004
    #4
  5. Bob

    abhi19_11

    Joined:
    Feb 17, 2009
    Messages:
    1
    Plz Help Me

    Hello Guys i want to send the line by line from a text file
    i.e. 1st line will send first and 2nd line will send second time and so on

    here is the code for sending random line, and i dont want to send random line
    I want to send Line By Line


    private void selectAMsg() throws FileNotFoundException, IOException,
    MessagingException
    {

    // getting the message file path from input and making buffered Reader
    // object
    BufferedReader msgFile = new BufferedReader(
    new FileReader("C://tips.txt"));

    // creating empty arraylist for message
    List msgs = new ArrayList();

    String str = "";
    // Loop that read every line from the message file and put that in to
    // message arraylist
    while ((str = msgFile.readLine()) != null)
    {
    if (str.trim().length() != 0)
    {
    msgs.add(str);
    }
    }

    // Getting random int in the range of the element of arraylist
    int randomInt = (int)(Math.random() * msgs.size());

    // Getting random message
    String randomMessage = (String)msgs.get(randomInt);

    sendMail(randomMessage);

    }

    Please Reply me Soon I m in trouble
    Thank You
     
    abhi19_11, Feb 17, 2009
    #5
  6. Bob

    tuldom84

    Joined:
    Jul 15, 2012
    Messages:
    1
    Simple Solution to File Read

    I know this post was years ago, however I am a new programmer and was trying to accomplish the same thing. I'm sure someone else will find this post just as I have. I wanted to accomplish reading text in from a file, storing it to a String while keeping the exact format which the text file has, all while being simple and easy to remember. Believe it or not I could not find any good examples of this so I came up with my own code.
    Here it is:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;

    public class ReadFromFile {
    static File file1 = new File("C:\\newDir\\test.txt");
    static FileReader fileReader;

    public static void main(String[] args) throws IOException {

    fileReader = new FileReader(file1);

    BufferedReader br = new BufferedReader(fileReader);

    Scanner scan = new Scanner(br);

    while (scan.hasNext()) { // iterate through all tokens in the text file

    String str = new String(); // create a new string

    str = scan.nextLine();// while scanning text, store each line to a
    // new string

    try {

    System.out.println(str); // print all lines to console

    } catch (Exception e) {

    e.printStackTrace();
    }

    }

    }

    }
     
    tuldom84, Jul 15, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rkr
    Replies:
    5
    Views:
    1,223
  2. Nishi Bhonsle
    Replies:
    1
    Views:
    938
    Thomas Weidenfeller
    Jul 20, 2004
  3. R
    Replies:
    8
    Views:
    3,909
    Tom Forsmo
    Oct 21, 2006
  4. Murali
    Replies:
    2
    Views:
    606
    Jerry Coffin
    Mar 9, 2006
  5. Replies:
    12
    Views:
    14,173
    Roedy Green
    Jun 29, 2007
Loading...

Share This Page