Transfer from ruthless
Java one line writes or reads data
Links: http://www.cnblogs.com/linjiqin/archive/2011/03/23/1992250.html
If there is a evdokey_201103221556.txt file under the E:/phsftp/evdokey directory,
The Evdokey_201103221556.txt file is now written or read, and the garbled problem of writing or reading is resolved.
File contents such as:
zhangsan,23, Fujian
LISI,30, Shanghai
wangwu,43, Beijing
laolin,21, Chongqing
ximenqing,67, Guizhou
The code is as follows:
/** * A line to read the file, suitable for character reading, if you read Chinese characters will appear garbled * stream of the closing order: first open the back off, after opening the first off, * otherwise it may appear java.io.IOException:Stream Closed Exception * * @throws IOException */@Test public void readFile01 () throws IOException {Filerea Der fr=new FileReader ("E:/phsftp/evdokey/evdokey_201103221556.txt"); BufferedReader br=new BufferedReader (FR); String line= ""; String[] Arrs=null; while ((Line=br.readline ())!=null) {Arrs=line.split (","); System.out.println (Arrs[0] + ":" + arrs[1] + ":" + arrs[2]); } br.close (); Fr.close (); /** * Read the file one line at a time to resolve the reading of Chinese characters garbled * stream of the closing order: first open the back off, after opening the first off, * otherwise it may appear java.io.IOException:Strea M closed Exception * * @throws ioexception * * @Test public void ReadFile02 () throws IOException {Filei Nputstream fis=new FileInputStream ("E:/phsftp/evdokey/evdokey_201103221556.txt"); InputStreamReader isr=new inputstreamreader (FIS, "UTF-8 "); BufferedReader br = new BufferedReader (ISR); Abbreviated as follows//bufferedreader BR = new BufferedReader (new InputStreamReader (//New FileInputStream ("E:/phs Ftp/evdokey/evdokey_201103221556.txt ")," UTF-8 "); String line= ""; String[] Arrs=null; while ((Line=br.readline ())!=null) {Arrs=line.split (","); System.out.println (Arrs[0] + ":" + arrs[1] + ":" + arrs[2]); } br.close (); Isr.close (); Fis.close (); }/** * Write a line to the file, suitable for character writing, if written in Chinese characters will appear garbled * stream of the closing order: first open the back off, after opening the first off, * otherwise it may appear java.io.IOException : Stream Closed Exception * * @throws IOException */@Test public void writeFile01 () throws IOException { String[] arrs={"Zhangsan,23,fujian", "Lisi,30,shanghai", "wangwu,43,beijing", "Laolin,21,chongqing", "Ximenqing,67,guizhou"}; FileWriter fw=new FileWriter (The New File ("e:/pHsftp/evdokey/evdokey_201103221556.txt ")); Garbled BufferedWriter bw=new BufferedWriter (FW) will appear when writing Chinese characters. BufferedWriter bw=new BufferedWriter (New BufferedWriter (New OutputStreamWriter (New FileOutputStream ("E:/ Phsftp/evdokey/evdokey_201103221556.txt "))) (" UTF-8 ")); for (String Arr:arrs) {bw.write (arr+ "\t\n"); } bw.close (); Fw.close (); }/** * Writes a line to the file, resolving garbled characters when writing to Chinese characters * Stream closing order: First open the back off, then open the first off, * otherwise it may appear java.io.IOException:Strea M closed Exception * * @throws ioexception * * @Test public void WriteFile02 () throws IOException {Stri Ng[] arrs={"zhangsan,23, Fujian", "lisi,30, Shanghai", "wangwu,43, Beijing", "l aolin,21, Chongqing "," ximenqing,67, Guizhou "}; Solve Chinese garbled problem when writing to FileOutputStream Fos=new FileOutputStream (New File ("E:/phsftp/evdokey/evdokey_201103221556.txt") ); OutputStreamWriter osw=new OutputStreamWriter (FOS, "UTF-8"); BufferedWriter bw=new BufferedWriter (OSW); Abbreviated as follows://bufferedwriter writer = new BufferedWriter (new OutputStreamWriter (//New FileOutputStream ( New File ("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8"); for (String Arr:arrs) {bw.write (arr+ "\t\n"); }//Note Close the order, first open after closing, after opening the first closed Bw.close (); Osw.close (); Fos.close (); }
Java Note--java Write or read data on a line