Read data from a text file using Java

后端 未结 16 1503
挽巷
挽巷 2020-12-10 05:35

I need to read a text file line by line using Java. I use available() method of FileInputStream to check and loop over the file. But while reading,

相关标签:
16条回答
  • 2020-12-10 06:01

    Yes, buffering should be used for better performance. Use BufferedReader OR byte[] to store your temp data.

    thanks.

    0 讨论(0)
  • 2020-12-10 06:02

    If you want to read line-by-line, use a BufferedReader. It has a readLine() method which returns the line as a String, or null if the end of the file has been reached. So you can do something like:

    BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
    String line;
    while ((line = reader.readLine()) != null) {
     // Do something with line
    }

    (Note that this code doesn't handle exceptions or close the stream, etc)

    0 讨论(0)
  • 2020-12-10 06:03

    How about using Scanner? I think using Scanner is easier

         private static void readFile(String fileName) {
           try {
             File file = new File(fileName);
             Scanner scanner = new Scanner(file);
             while (scanner.hasNextLine()) {
               System.out.println(scanner.nextLine());
             }
             scanner.close();
           } catch (FileNotFoundException e) {
             e.printStackTrace();
           }
         }
    

    Read more about Java IO here

    0 讨论(0)
  • 2020-12-10 06:07
    public class ReadFileUsingFileInputStream {
    
    /**
    * @param args
    */
    static int ch;
    
    public static void main(String[] args) {
        File file = new File("C://text.txt");
        StringBuffer stringBuffer = new StringBuffer("");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            try {
                while((ch = fileInputStream.read())!= -1){
                    stringBuffer.append((char)ch);  
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("File contents :");
        System.out.println(stringBuffer);
        }
    }
    
    0 讨论(0)
  • 2020-12-10 06:07
    public class FilesStrings {
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("input.txt");
        InputStreamReader input = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(input);
        String data;
        String result = new String();
    
        while ((data = br.readLine()) != null) {
            result = result.concat(data + " ");
        }
    
        System.out.println(result);
    
    0 讨论(0)
  • 2020-12-10 06:11

    In Java 8 you could easily turn your text file into a List of Strings with streams by using Files.lines and collect:

    private List<String> loadFile() {
        URI uri = null;
        try {
            uri = ClassLoader.getSystemResource("example.txt").toURI();
        } catch (URISyntaxException e) {
            LOGGER.error("Failed to load file.", e);
        }
        List<String> list = null;
        try (Stream<String> lines = Files.lines(Paths.get(uri))) {
            list = lines.collect(Collectors.toList());
        } catch (IOException e) {
            LOGGER.error("Failed to load file.", e);
        }
        return list;
    }
    
    0 讨论(0)
提交回复
热议问题