Read and Write files in java
// Byte stream import java. io. *; public class testchaer {public static void main (String args []) {FileReader fr = null; FileWriter fw = null; try {fr = new FileReader ("C: /Users/Administrator/Desktop/xuexi/jiankongjiao. py "); fw = new FileWriter (" C:/Users/Administrator/Desktop/xuexi/xuexi11.txt "); char [] buffer = new char [100]; while (true) {int tem = fr. read (buffer, 0, buffer. length); if (tem =-1) {break;} fw. write (buffer, 0, tem) ;}} catch (Exception e) {System. out. println (e);} finally {try {fr. close (); fw. close () ;}catch (Exception e2) {// TODO: handle exception }}}}
// Upload stream import java. io. *; public class inpur {public static void main (String args []) {FileInputStream FD = null; FileOutputStream fos = null; try {FS = new FileInputStream ("C: /Users/Administrator/Desktop/xuexi/jiankongjiao. py "); fos = new FileOutputStream (" C:/Users/Administrator/Desktop/xuexi/xuexi2.txt "); byte [] buffer = new byte [100]; while (true) {int temp = FCM. read (buffer, 0, buffer. length); if (temp =-1) {break;} fos. write (buffer, 0, temp); String stri = new String (buffer); // call the tim method of the string object, the Ending Space of string and the empty string stri = stri. trim (); System. out. println (stri) ;}} catch (Exception e) {// TODO: handle exception System. out. println (e);} finally {try {FCM. close (); fos. close ();} catch (Exception e) {System. out. println (e );}}}}
One row is read,
package ifwh;import java.io.*;public class teatsjaa { public static void main(String agrs[]) { FileReader fileReader=null; BufferedReader bufferedWriter=null; try { fileReader=new FileReader("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py"); bufferedWriter =new BufferedReader(fileReader); String line=null; while (true) { line=bufferedWriter.readLine(); if (line==null){ break; } System.out.println(line); } } catch (Exception e) { // TODO: handle exception System.out.println(e); }finally { try { fileReader.close(); bufferedWriter.close(); } catch (Exception e2) { // TODO: handle exception System.out.println(e2); } }}}