Read data from a text file using Java

后端 未结 16 1581
挽巷
挽巷 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:11

    Simple code for reading file in JAVA:

    import java.io.*;
    
    class ReadData
    {
        public static void main(String args[])
        {
            FileReader fr = new FileReader(new File(""));
            while(true)
            {
                int n=fr.read();
                if(n>-1)
                {
                    char ch=(char)fr.read();
                    System.out.print(ch);
                }
            }
        }
    }
    

提交回复
热议问题