Java---IO流DateXxxStream

3/8/2017来源:ASP.NET技巧人气:1919

package cn.hncu.io.demo2; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JOptionPane; public class readWriteInt { public static void main(String[] args) { writeDemo(); readDemo(); } PRivate static void readDemo() { try { DataInputStream in = new DataInputStream(new FileInputStream("d:/a/files/int.txt")); while(in.available()>0){//判断文件是否读取到末尾的方法 if(in.readInt()>1 && in.readInt()%50==0){ System.out.println(); } System.out.print(in.readInt()+" "); } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "文件没有找到!"); } catch (IOException e) { System.out.println("文件读取时出现异常!"); } } private static void writeDemo() { File file = new File("d:/a/files/int.txt"); DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream(file)); } catch (FileNotFoundException e1) { System.out.println("文件不存在!"); } for (int i = 1; i < 300; i++) { try { out.writeInt(i); } catch (IOException e) { System.out.println("写文件时出现异常!"); } } } }