publicstaticvoidmain(String[] args) try File a =new File("D:/file1.txt"); File b= new File("D:/file3.txt"); FileInputStream c = new FileInputStream(a); FileOutputStream d = new FileOutputStream(b); int e; while((e= c.read())!= -1 ) d.write(e); c.close(); d.close(); catch(FileNotFoundException e) System.out.println("FileStreamsTest:" + e); catch(IOException e) System.err.println("FileStreamTest:" + e);
以字节流的形式写入文档数据
try File f = new File("C:\Users\Administrator\Desktop\1.txt"); FileOutputStream f1 = new FileOutputStream(f); byte [] a =; f1.write(a); f1.close();
catch(IOException e) e.printStackTrace();
首先创建的一个文档对象,提供目的地。再创建一个文档流出的对象,两者相联系,然后讲
创建并删除文档
File f1 = new File("D:\1.txt"); File f2 = new File("D:\","2.txt"); File f3 = new File("D:\",File.separator+"1.txt"); f2.createNewFile(); f3.delete();
复制粘贴
try File f1 = new File("D:\3.txt"); File f2 = new File("5.txt"); FileInputStream in = new FileInputStream(f1);//创建一个输入流,实现从磁盘写入内存中 FileOutputStream out = new FileOutputStream(f2);//创建一个输出流,实现从内存写入磁盘中 int c; while((c = in.read())!=-1) out.write(c); in.close(); out.close(); catch(IOException e) e.printStackTrace();
读取字符信息
try FileReader file = new FileReader("D:\3.txt"); int data = 0; while((data= file.read())!=-1) System.out.print((char)data); file.close(); catch(IOException e) e.printStackTrace();