Seperti yang ditunjukkan di bawah ini:
Paket Salinan; import java.io.bufferedInputStream; impor java.io.bufferedoutputstream; import java.io.bufferedreader; impor java.io.bufferedwriter; impor java.io.fileinputstream; impor java.io.filenotfoundexception; impor java.filenoMoundexception; java.io.filereader; import java.io.filewriter; impor java.io.ioException; kelas publik filecopy {public static void main (string [] args) melempar ioException { // Tipe pertama: Gunakan filereader dan fileWrite untuk membaca satu karakter pada waktu filereader fr = filereader baru ("d: //a.txt"); fileWriter fw = fileWriter baru ("d: //b.txt"); int ch; while ((ch = fr.read ())! = {fw.write (ch);} fw.close (); fr.close (); // The second type: Use FileReader and FileWrite to read one character array at a time FileReader fr = new FileReader("D://a.txt");FileWriter fw = new FileWriter("D://b.txt");char[] chs = new char[1024];int len;while((len = fr.read(chs)) != -1) {fw.write (chs, 0, len);} fw.close (); fr.close (); // Tipe ketiga: Gunakan FileOutputStream dan FileInputStream, baca satu byte pada waktu fileInputStream fis = fileInputStream baru ("d: //a.txt"); fileOutputStream fos = FileOutputStream baru ("D: //B.TXT"); int ch; while ((che = fis. {fos.write (ch);} fos.close (); fis.close (); // Jenis Keempat: Gunakan FileOutputStream dan FileInputStream untuk membaca satu byte array pada waktu fileInputStream fis = fileInputStream baru ("d: //a.txt"); fileOutputStream fos = byte new baru ("d: //b.txt"); int ch; byte baru [] byte = byte = byte baru ("d: //b.txt"); int ch; byte [] byte [] byte [] byte ("byte baru (" byte ("d: //b.txt"); int ch; byte [] byte [] byte [] byte [] byte (by = byte ("byte (" byte ("d: /b.b.txt"); int ch; byte [] byte = byte New while ((ch = fis.read (oleh))! = -1) {fos.write (oleh, 0, ch);} fos.close (); fis.close (); // The fifth type: Use BufferedReader and BufferedWriter to read one line at a time BufferedReader br = new BufferedReader(new FileReader("D://a.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("D://b.txt"));String line;while((line = br.readLine()) != null) {bw.write (line); bw.newline (); bw.flush ();} bw.close (); br.close (); // The sixth type: Use efficient buffered streams, BufferedInputStream and BufferedOutputStream, read one byte at a time BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D://a.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D://b.txt"));int ch;while((ch = bis.read ())! = -1) {bos.write (ch);} bos.close (); bis.close (); // The seventh type: Use efficient buffered streams, BufferedInputStream and BufferedOutputStream, read one byte array at a time BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D://a.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D://b.txt"));int ch; byte [] oleh = byte baru [1024]; while ((ch = bis.read (oleh))! = -1) {bos.write (oleh, 0, ch);} bos.close (); bis.close ();}}
Ringkasan 7 cara menyalin teks Java adalah semua konten yang saya bagikan dengan Anda. Saya harap Anda dapat memberi Anda referensi dan saya harap Anda dapat mendukung wulin.com lebih lanjut.