此处省略介绍,
package org.rui.io.inout; import java.io.BufferedReader; import java.io.FileReader; /** * 缓冲输入文件 * @author lenovo * */ public class BufferedInputFile { public static String read(String filename) throws Exception { BufferedReader br=new BufferedReader( new FileReader(filename) ); String s; StringBuilder sb=new StringBuilder(); while((s=br.readLine())!=null) sb.append(s+"\n"); br.close(); // getClass(); return sb.toString(); } public static void main(String[] args) throws Exception { //String path=BufferedInputFile.class.getResource(".").getFile().toString(); //System.out.println(read(path+"BufferedInputFile.java")); String path="D:/Users/liangrui/workspace/thinking/src/org/rui/io/inout/"; System.out.println(read(path+"BufferedInputFile.java")); } } /** output: 同 上 */
package org.rui.io.inout; import java.io.*; /** * 从内存输入 * @author lenovo * */ public class MemoryInput { public static void main(String[] args) throws Exception { String path="D:/Users/liangrui/workspace/thinking/src/org/rui/io/inout/"; // BufferedInputFile.read(path+"MemoryInput.java") StringReader in=new StringReader(BufferedInputFile.read(path+"MemoryInput.java")); int c = 0; while((c=in.read())!=-1) System.out.print((char)c); } } /** output: 同 上 */
package org.rui.io.inout; import java.io.ByteArrayInputStream; import java.io.DataInputStream; /** * 格试化的内存输入 * @author lenovo * */ public class FormattedMemoryInput { public static void main(String[] args) { try { String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\inout/"; DataInputStream in=new DataInputStream( new ByteArrayInputStream( BufferedInputFile.read(path+"FormattedMemoryInput.java").getBytes() )); while(true) System.out.print((char)in.readByte());//返回值不能检测输入的是否结束 } catch (Exception e) { System.out.println("end of stream"); } } }
package org.rui.io.inout; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.FileInputStream; /** * 下面演示了怎样一次一个字节地的读取文件 * @author lenovo * */ public class TestEOF { public static void main(String[] args) throws Exception { String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\inout/"; DataInputStream in=new DataInputStream( new BufferedInputStream( new FileInputStream(path+"TestEOF.java") )); //注意 会随着所读的媒介类型的不同而有所不同,字面的意思就是 在没有阻塞的情况下所能读取的字节数 //对于文件 这意味着整个文件,但是对于不同类型的流,可能就不是这样的,因此要谨慎使用 while(in.available()!=0) System.out.print((char)in.readByte()); } }
package org.rui.io.inout; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileWriter; import java.io.PrintWriter; import java.io.StringReader; /** * 基本的文件输出 * @author lenovo * */ public class BasicFileOutput { public static void main(String[] args) throws Exception { String file="D:/BasicFileOutput.out"; String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\inout/"; //读文件 BufferedReader in =new BufferedReader( new StringReader( BufferedInputFile.read(path+"BasicFileOutput.java") ) ); //写 PrintWriter pw=new PrintWriter( new BufferedWriter(new FileWriter(file)) ); //// int linecount=1; String s; while((s=in.readLine())!=null) pw.println(linecount+++s); pw.close(); //读已写入的文件 System.out.println(BufferedInputFile.read(file)); } }
package org.rui.io.inout; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; import java.io.StringReader; /** * 文本文件输出的快揵方式 * @author lenovo * */ public class FileOutputShortcut { public static void main(String[] args) throws Exception { String file="D:/BasicFileOutput.out"; String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\inout/"; //读文件 BufferedReader in =new BufferedReader( new StringReader( BufferedInputFile.read(path+"BasicFileOutput.java") ) ); //写 java se5在printWriter中添加 了一个辅助构造器 PrintWriter pw=new PrintWriter(file); //// int linecount=1; String s; while((s=in.readLine())!=null) pw.println(linecount+++s); pw.close(); //读已写入的文件 System.out.println(BufferedInputFile.read(file)); } }
package org.rui.io.inout; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; /** * 存储和恢复数据 * 如果我们使用了dataoutputstream写入数据,java保证我们可以使用datainputstream准确地读取数据, * 无论读和写数据平台多么不同 * utf-8是一种多字节格式,其编码长度根据实际使用的字符集会有所变化,如果我们的只是ASCII只占7位 * 浪费空间和带宽,所以utf-8将ASCII字符编码成单一字节的形式,而非ASCII字符则编码成两到三个字节的形式 * @author lenovo * */ public class StoringAndRecoveringData { public static void main(String[] args) throws Exception { String path="d:/data.txt"; //输出 DataOutputStream out=new DataOutputStream( new BufferedOutputStream( new FileOutputStream(path) ) ); out.writeDouble(5.55); out.writeUTF("hello world"); out.writeDouble(6.66); out.writeUTF("你好 世界"); out.close(); //读取 DataInputStream in=new DataInputStream( new BufferedInputStream( new FileInputStream(path) ) ); // System.out.println(in.readDouble()); System.out.println(in.readUTF()); System.out.println(in.readDouble()); System.out.println(in.readUTF()); } } /**output: 5.55 hello world 6.66 你好 世界 */
package org.rui.io.inout; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.RandomAccessFile; /** * 读写随机访问文件 * * 使用RandomAccessFile时,你必须知道文件排版,这样才能正确地操作它 * 拥有读取基本类型和utf-8字符串的各种具体方法,下面示例 * * 你可能会考虑 使用 内存映射文件 来贷替randomaccessfile * @author lenovo * */ public class UsingRandomAccessFile { static String file="rtest.dat"; static void display() throws Exception { RandomAccessFile rf=new RandomAccessFile(file,"r"); for(int i=0;i<7;i++) System.out.println( "value "+i+" : "+rf.readDouble()); System.out.println(rf.readUTF()); rf.close(); } public static void main(String[] args) throws Exception { RandomAccessFile rf=new RandomAccessFile(file,"rw"); //写 for(int i=0;i<7;i++) rf.writeDouble(i*1.414); rf.writeUTF(" the end of the file"); rf.close(); //打印 display(); //写 打开并修改 rf=new RandomAccessFile(file,"rw"); rf.seek(5*8); rf.writeDouble(47.0001); rf.close(); //打印 display(); } } /**output: value 0 : 0.0 value 1 : 1.414 value 2 : 2.828 value 3 : 4.242 value 4 : 5.656 value 5 : 7.069999999999999 value 6 : 8.484 the end of the file value 0 : 0.0 value 1 : 1.414 value 2 : 2.828 value 3 : 4.242 value 4 : 5.656 value 5 : 47.0001 value 6 : 8.484 the end of the file */