简体   繁体   中英

how to read next line in .txt file

may i know why i am unable to read next line in a .txt file when i upload it. it happens to all the .txt file that i try to upload and then system.out.println() with. in my text file it contains : cats,dogs,monkey ( each in one line ) .. but the value out put is:

[Monkey][Monkey, null][Monkey, null, null][Monkey, null, null, null][Monkey, null, null, null, null][Monkey, null, null, null, null, null][Monkey, null, null, null, null, null, null]

needs help on this. thank you.

and wondering why i can read .txt file and not .doc. need advise on this as well.

import java.awt.List;
import java.io.*;  
import java.util.ArrayList;

import javax.imageio.IIOException;
import javax.swing.JFileChooser;

public class searchforCapitalLetter {

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


          try {  

                // file chooser
              JFileChooser chooser=new  JFileChooser();
            int returnVal = chooser.showOpenDialog(null);{
            if(returnVal == JFileChooser.APPROVE_OPTION)

            {   File f = chooser.getSelectedFile();}
            FileInputStream fin = new FileInputStream(chooser.getSelectedFile());
            DataInputStream din = new DataInputStream(fin);    
       BufferedReader br = new BufferedReader(new InputStreamReader(din)); 

    ArrayList<String>list =new ArrayList<String> ();

    if ((br.readLine()) != null) {
    while (br.readLine() != " ") {
        list.add(br.readLine());
        System.out.print (list);
    } br.close() ;
    }//closes if statement
     } // closes method dialog
                } // closes try method

         catch (FileNotFoundException e) {
                    e.printStackTrace();
                }     // closes catch method
            } // closes method body

} // closes class method

Per the bufferedreader api:

public String readLine() throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed.

Returns : A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Throws : IOException - If an I/O error occurs

Note that your code is making a few errors:

1) it is calling readline() once to check a return value and again to use the return value. The first call (in the while condition) removes the current value from the buffer, so your are dropping every other line.

2) you are using the wrong comparison to see if there's data remaining. x != y, where both x and y are objects, checks for pointer equality - is the memroy location where x is allocated the same as the memory location where y is allocated. what you really want is to store the value in a variable.

For example:

BufferedReader in = ...;
String s = in.readLine();
while(s!=null){
    ...
    s = in.readLine();
}

The reason you can't use .doc is because .doc files are formatted, and as such, have some things your code would have to parse to read from them.

As far as the weird printing goes, you read the code twice before you even get to printing it (each time you call .readLine, it moves the scanner to the next line). Try the following code:

ArrayList<String>list =new ArrayList<String> ();
String currentLine;
while ((currentLine = br.readLine()) != null) {
    list.add(currentLine);
    System.out.println(currentLine);
} br.close() ;
}

That will keep track of the current line in a variable, rather than repeatedly moving the scanner to the next line. The other issue was it will be null, not " ", when the end of the file is reached

A classic BufferedReader to read content from a file. this our file to read with Java:

This is the content to write into file

This is the content to write into file

package com.stackoverflow;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile{

    private static final String FILENAME = "D:\\test\\filename.txt";

    public static void main(String[] args) {

        BufferedReader br = null;
        FileReader fr = null;

        try {

            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);

            String sCurrentLine;

            br = new BufferedReader(new FileReader(FILENAME));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

    }

}

Output :

This is the content to write into file

This is the content to write into file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM