Java IO流相关知识代码解析

这篇文章主要介绍了Java IO流相关知识代码解析,具有一定借鉴价值,需要的朋友可以参考下。

一、IO流的分类

字符流

Reader
InputStreamReader(节点流)
BufferedReader(处理流)
Writer
OutputStreamWriter(节点流)
BufferedWriter(处理流)
PrintWriter

字节流

InputStream
FileInputStream(节点流)
BufferedInputStream(处理流)
ObjectInputStream(处理流)
PrintStream
OutputStream
FileOutputStream(节点流)
BufferedOutputStream(处理流)
ObjectOutputStream(处理流)

断点处理的流

RandomAccessfile

二、IO流的用法

1、转换流的用法

 FileInputStream in = new FileInputStream(newFile("")); Readerreader = new InputStreamReader(in);//字节转字符 FileOutputStreamout = new FileOutputStream(newFile("")); Writer writer = new OutputStreamWriter(out);//字符转字节

2、对象序列化,对象需要实现Serializable接口

 FileOutputStreamfileOutputStream = new FileOutputStream("C:\\Users\\lx\\Desktop\\Record.txt"); ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(object);//向指定文件写入对象object objectOutputStream.close(); FileInputStreamfileInputStream = new FileInputStream("C:\\Users\\lx\\Desktop\\Record.txt"); ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream); object = objectInputStream.readObject();//读取得到对象object fileInputStream . lose();

3、断点的运用

 public class Copy extends Thread{ //可以利用多线程实现拷贝 longstart; longend; Filesorce; Filetargetdir; publicCopy() { } publicCopy(longstart,long end, File sorce, File targetdir) { //利用构造方法传递需要拷贝的长度,拷贝开始位置,以及目标文件和源文件 super(); this.start= start; this.end= end; this.sorce= sorce; this.targetdir= targetdir; } @Override publicvoid run(){ try{ RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r"); RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw"); souceRaf.seek(start); targetRaf.seek(start); intlen= 0; byte[]bs = new byte[1024]; longseek; System.out.println(start+"---->"+end+this.getName()); while((len= souceRaf.read(bs))!=-1){ targetRaf.write(bs, 0, len); seek= souceRaf.getFilePointer(); //获取断点位置 if(seek== end){ break; } } targetRaf.close(); souceRaf.close(); } catch (IOException e) { e.printStackTrace(); } } }

4、字节流的用法

 public class Test_InputStream { //利用字节流获取文本文件内容,但是容易出现问题 /* //可能出现int长度越界 public static void main(String[] args) throws IOException { InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt")); byte[] b = new byte[inputStream.available()]; inputStream.read(b); String str = new String(b); System.out.println(str); } */ //可能出现乱码 public static void main(String[] args) throws IOException { File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt"); InputStream inputStream = new FileInputStream(file); //统计每次读取的实际长度 int len = 0; //声明每次读取1024个字节 byte[] b = new byte[2]; StringBuffer sBuffer = new StringBuffer(); while((len=inputStream.read(b))!=-1){ sBuffer.append(new String(b,0,len)); } System.out.println(sBuffer.toString()); } } //利用字节流拷贝文件 public void copy(File sourceFile, File targetDir) { // FileInputStreamfileInputStream = null; FileOutputStreamfileOutputStream = null; fileInputStream= new FileInputStream(sourceFile); FiletargetFile = new File(targetDir,sourceFile.getName()); fileOutputStream= new FileOutputStream(targetFile); byte[]b = new byte[1024]; intlen = 0; while((len= fileInputStream.read(b)) != -1) { fileOutputStream.write(b, 0, len); } }

5、缓存字符流的用法

 publicstatic void main(String[] args) throws IOException { //缓存字符流实现写入文件 InputStreamin = System.in; Readerreader = new InputStreamReader(in); BufferedReaderbr = new BufferedReader(reader); BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt"))); Strings=""; while((s=br.readLine())!=null) { bw.write(s); bw.newLine(); bw.flush(); //字符流千万不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!! } }

总结

以上就是本文关于Java IO流相关知识代码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

以上就是Java IO流相关知识代码解析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java