CopyFile
package org.wp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFile { public static void main(String args[]) { try { FileInputStream fis = new FileInputStream("F:\\test.jpg"); FileOutputStream fos = new FileOutputStream("F:\\temp.jpg"); // 返回读取文件的内容转换为int形式 int read = fis.read(); while (read != -1) { fos.write(read); read = fis.read(); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream inStream = new FileInputStream("F:/test.jpg"); FileOutputStream outStream = new FileOutputStream( "F:/temporary.jpg"); byte[] buffer = new byte[1024]; // 返回实际读取到文件内容的长度 int length = inStream.read(buffer); while (length != -1) { // 如果不指定写入的文件长度,文件大小会超出复制的文件 outStream.write(buffer, 0, length); length = inStream.read(buffer); } inStream.close(); outStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream fileInputStream = new FileInputStream("F:/test.jpg"); FileOutputStream fileOutputStream = new FileOutputStream( "F:/occasional .jpg"); byte[] buffer = new byte[1024]; // 设置每次读取的长度 int length = fileInputStream.read(buffer, 0, 20); while (length != -1) { // 设置每次写入的长度 fileOutputStream.write(buffer, 0, 20); length = fileInputStream.read(buffer, 0, 20); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
ReaderWriterTest
package org.wp.io; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class ReaderWriterTest { public static void main(String arg[]) { try { FileReader fr = new FileReader("F:/test.txt"); FileWriter fw = new FileWriter("F:/temp.txt"); int read = fr.read(); while (read != -1) { fw.write(read); read = fr.read(); } fr.close(); fw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
BufferedReaderWriterTest
package org.wp.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class BufferedReaderWriterTest { public static void main(String args[]) { try { FileReader fr = new FileReader("F:/test.txt"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("F:/temp.txt"); BufferedWriter bw = new BufferedWriter(fw); // 读取一行的内容,但不包含换行符 String str = br.readLine(); while (str != null) { bw.write(str); // 换行 bw.newLine(); str = br.readLine(); } br.close(); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileReader fileReader = new FileReader("F:/test.txt"); // BufferedReader(Reader in, int sz) // 创建一个使用指定大小输入缓冲区的缓冲字符输入流 BufferedReader bufferedReader = new BufferedReader(fileReader, 1024); FileWriter fileWriter = new FileWriter("F:/temporary.txt"); // BufferedWriter(Writer out, int sz) // 创建一个使用给定大小输出缓冲区的新缓冲字符输出流 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter, 1024); String s = bufferedReader.readLine(); while (s != null) { bufferedWriter.write(s); bufferedWriter.newLine(); s = bufferedReader.readLine(); } bufferedReader.close(); bufferedWriter.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
InputStreamReaderOutputStreamWriterTest
package org.wp.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class InputStreamReaderOutputStreamWriterTest { public static void main(String args[]) { try { FileInputStream fis = new FileInputStream("F:/test.txt"); // 默认为操作系统的字符编码 // 可以设置使用的字符编码 // isr = new InputStreamReader(fis, "UTF-8"); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); FileOutputStream fos = new FileOutputStream("F:/temp.txt"); // 默认为操作系统的字符编码 // 可以设置使用的字符编码 // isw = new OutputStreamWriter(fos, "UTF-8"); OutputStreamWriter isw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(isw); String str = br.readLine(); while (str != null) { bw.write(str); bw.newLine(); str = br.readLine(); } br.close(); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
PrintStreamWriterTest
package org.wp.io; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; /** * PrintStream的方法不抛出IOException 只能封装OutputStream类型的字节流 * * PrintWriter的方法不抛出IOException 既可以封装OutputStream类型的字节流,也可以封装Writer类型的字符输出流 * * @author wp * */ public class PrintStreamWriterTest { public static void main(String args[]) { try { FileInputStream fis = new FileInputStream("F:/test.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); FileOutputStream fos = new FileOutputStream("F:/temp.txt"); PrintStream ps = new PrintStream(fos); String str = br.readLine(); while (str != null) { ps.println(str); str = br.readLine(); } br.close(); ps.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(new FileInputStream("F:/test.txt"))); FileWriter fw = new FileWriter("F:/temporary.txt"); PrintWriter pw = new PrintWriter(fw); String s = bufferedReader.readLine(); while (s != null) { pw.println(s); s = bufferedReader.readLine(); } bufferedReader.close(); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
DataInputOutputTest
package org.wp.io; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * DataInputStream能够以一种与机器无关的方式,直接从底层字节输入流读取Java基本类型和String类型的数据 * * DataOutputStream能够直接将Java基本类型和String类型的数据写出到其他的字节输出流 * * @author wp * */ public class DataInputOutputTest { public static void main(String args[]) { try { FileOutputStream fos = new FileOutputStream("F:/myfile.data"); DataOutputStream dos = new DataOutputStream(fos); dos.writeUTF("麦迪"); dos.writeInt(50); dos.close(); FileInputStream fis = new FileInputStream("F:/myfile.data"); DataInputStream dis = new DataInputStream(fis); System.out.println(dis.readUTF()); System.out.println(dis.readInt()); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
CharArrayReaderTest
package org.wp.io; import java.io.CharArrayReader; import java.io.IOException; import java.io.Reader; public class CharArrayReaderTest { public static void main(String args[]) { char[] b = { 'I', ' ', 'a', 'm', ' ', 'S', 'a', 'i', 'l', 'i', 'n', 'g', '!' }; CharArrayReader car = new CharArrayReader(b); new MyReader().getInfo(car); } } class MyReader { public void getInfo(Reader reader) { try { int read = reader.read(); while (read != -1) { System.out.print((char) read); read = reader.read(); } System.out.println(); } catch (IOException e) { e.printStackTrace(); } } }
CharArrayWriterTest
package org.wp.io; import java.io.CharArrayWriter; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; public class CharArrayWriterTest { public static void main(String args[]) { try { FileReader fr = new FileReader("F:/ooo.txt"); int i = fr.read(); while (i != -1) { System.out.println((char) i); i = fr.read(); } } catch (IOException e) { CharArrayWriter caw = new CharArrayWriter(); // true指定追加方式 PrintWriter pw = new PrintWriter(caw, true); e.printStackTrace(pw); String info = caw.toString(); Date time = new Date(); // 可以将time和info信息写入数据库 System.out.println("出错时间:" + time); System.out.println("错误信息:\n" + info); } } }