㈠ java中怎樣從文件中讀取數據
分為讀位元組,讀字元兩種讀法x0dx0a◎◎◎FileInputStream 位元組輸入流讀文件◎◎◎x0dx0apublic class Maintest {x0dx0ax0dx0apublic static void main(String[] args) throws IOException {x0dx0ax0dx0aFile f=new File("G:\\just for fun\\xiangwei.txt");x0dx0ax0dx0aFileInputStream fin=new FileInputStream(f);x0dx0ax0dx0abyte[] bs=new byte[1024];x0dx0ax0dx0aint count=0;x0dx0awhile((count=fin.read(bs))>0)x0dx0a{x0dx0ax0dx0aString str=new String(bs,0,count);//反復定義新變數:每一次都 重新定義新變數,接收新讀取的滾雹數據x0dx0ax0dx0aSystem.out.println(str);//反復輸出新變數:察槐每一次都 輸出重大沒帆新定義的新變數x0dx0a}x0dx0afin.close();x0dx0a}x0dx0a}x0dx0ax0dx0a◎◎◎FileReader 字元輸入流讀文件◎◎◎x0dx0apublic class Maintest {x0dx0apublic static void main(String[] args) throws IOException {x0dx0ax0dx0aFile f=new File("H:\\just for fun\\xiangwei.txt");x0dx0ax0dx0aFileReader fre=new FileReader(f);x0dx0ax0dx0aBufferedReader bre=new BufferedReader(fre);x0dx0ax0dx0aString str="";x0dx0awhile((str=bre.readLine())!=null)//●判斷最後一行不存在,為空x0dx0a{x0dx0aSystem.out.println(str);x0dx0a}x0dx0abre.close();x0dx0a fre.close();x0dx0ax0dx0a}x0dx0ax0dx0a}