Java输入输出处理技术1
1.保存用户输入到文件
从键盘读入一行字符,写到文件output.txt中去。
package io; import java.io.*; public class MyFileOutput { public static void main(String[] args) { FileInputStream fin; FileOutputStream fout; int ch; try { fin=new FileInputStream(FileDescriptor.in); fout=new FileOutputStream("output.txt"); System.out.println("请输入一行字符:"); while((ch=fin.read())!='\r') fout.write(ch); fin.close(); fout.close(); System.out.println("文件写入成功!"); } catch (FileNotFoundException e) { System.out.println("不能创建文件!"); }catch(IOException e){ System.out.println("输出流有误!"); } } }
2.显示文件内容
显示文本文件的内容。
package io; import java.io.*; public class TypeFile { public static void main(String[] args) { FileInputStream fin; FileOutputStream fout; int ch; if(args.length<1){ System.out.println("请指定文件名!"); return; } try { fin=new FileInputStream(args[0]); fout=new FileOutputStream(FileDescriptor.out); while((ch=fin.read())!=-1) fout.write(ch); fin.close(); fout.close(); } catch (FileNotFoundException e) { System.out.println("文件没有找到!"); } catch (IOException e) { System.out.println("输入流有误!"); } } }
3.文件的复制
文件复制程序。
package io; import java.io.*; public class CopyFile { public static void main(String[] args) { FileInputStream fin; FileOutputStream fout; int ch; if(args.length!=2){ System.out.println("参数格式不对,应该为:java CopyFile 原文件名 目标文件名"); return; } try { fin=new FileInputStream(args[0]); fout=new FileOutputStream(args[1]); while((ch=fin.read())!=-1) fout.write(ch); fin.close(); fout.close(); System.out.println("文件复制成功!"); } catch (FileNotFoundException e) { System.out.println("文件没有找到!"); } catch (IOException e) { System.out.println("读写文件有误!"); } } }
4.顺序输入流
顺序输入流示例。
package io; import java.io.*; import java.util.*; public class FileList implements Enumeration{ String MyFileList[]; int current=0; public FileList(String filelist[]){ MyFileList=filelist; } public FileList(){ MyFileList=null; } public boolean hasMoreElements() { if(MyFileList==null) return false; if(current<MyFileList.length) return true; else return false; } public Object nextElement() { FileInputStream in=null; if(!hasMoreElements()) return null; try { in = new FileInputStream(MyFileList[current]); current++; } catch (FileNotFoundException e) { System.out.println("Can't open file:"+MyFileList[current]); } return in; } }
package io; import java.io.*; import java.util.*; public class MySequenceIn { public static void main(String[] args) { FileList myList = new FileList(args); SequenceInputStream sin; FileOutputStream fout; int data; try { sin = new SequenceInputStream(myList); fout = new FileOutputStream(FileDescriptor.out); while((data=sin.read())!=-1) fout.write(data); sin.close(); fout.close(); } catch (FileNotFoundException e) { System.out.println("文件无法打开"); } catch (IOException e) { System.out.println("读写文件有误"); } } }
5.管道输入输出流
管道输入输出流示例。
package io; import java.io.*; //本线程类用于发送数据 public class ThreadOut extends Thread { PipedInputStream pin; PipedOutputStream pout; byte data[]={1,2,3}; public ThreadOut(PipedInputStream in, PipedOutputStream out){ pin = in; pout = out; } public void run(){ try { pout.write(data); } catch (IOException e) { // e.printStackTrace(); } } }
package io; import java.io.*; //本线程类用于接收数据 public class ThreadIn extends Thread { PipedInputStream pin; PipedOutputStream pout; int data; public ThreadIn(PipedInputStream in, PipedOutputStream out){ pin = in; pout = out; } public void run(){ try { while((data=pin.read())!=-1) System.out.println(data); } catch (IOException e) { // e.printStackTrace(); } } }
package io; import java.io.*; public class MyPipedIO { public static void main(String[] args) { PipedInputStream mypin=null; PipedOutputStream mypout=null; try { mypin = new PipedInputStream(); mypout = new PipedOutputStream(); mypin.connect(mypout); ThreadOut tout = new ThreadOut(mypin, mypout); ThreadIn tin = new ThreadIn(mypin, mypout); tout.start(); tin.start(); } catch (IOException e) { System.out.println("无法连接管道"); } } }
6.过滤输入输出流
数据输入输出流使用示例。
package io; import java.io.*; public class MyDataIO { public static void main(String[] args) { DataOutputStream dout; DataInputStream din; try { dout = new DataOutputStream(new FileOutputStream("testfile.dat")); dout.writeInt(100); dout.writeLong(123456789); dout.writeDouble(1.23456); dout.writeFloat(1.2f); dout.writeBoolean(true); din = new DataInputStream(new FileInputStream("testfile.dat")); System.out.println(din.readInt()); System.out.println(din.readLong()); System.out.println(din.readDouble()); System.out.println(din.readFloat()); System.out.println(din.readBoolean()); } catch (FileNotFoundException e) { System.out.println("没有找到文件!"); } catch (IOException e) { System.out.println("无法正常创建输入输出流数据!"); } } }