JAVA(IO流方法大全).doc_第1页
JAVA(IO流方法大全).doc_第2页
JAVA(IO流方法大全).doc_第3页
JAVA(IO流方法大全).doc_第4页
JAVA(IO流方法大全).doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package Stream;import java.io.*;public class Io public void test1() throws Exception File file=new File(E:/txt.txt); if(file.exists() System.out.println(是否是文件:+file.isFile(); System.out.println(文件名是:+file.getName(); System.out.println(路径是:+file.getPath(); System.out.println(绝对路径是:+file.getAbsolutePath(); System.out.println(上级目录是:+file.getParent(); System.out.println(文件大小是:+file.length()+字节); else file.createNewFile(); public void test2() throws Exception/以字节流方式读取 File file=new File(E:/txt1.txt); FileInputStream fi=new FileInputStream(file); byte content= new bytefi.available(); /*for(int i=0;icontent.length;i+) contenti=(byte)fi.read(); /读取长度后,将其转换成字符型 */ /第一种方式 fi.read(content); /第二种方式 String str=new String(content); System.out.println(str.trim(); public void test3() throws Exception/以字节流方式写入数据 File file=new File(E:/txt1.txt); FileOutputStream fo=new FileOutputStream(file); bytecontent=new String(你是一个).getBytes(); fo.write(content); content=new String(呵呵).getBytes(); fo.write(content); fo.close(); public void test4() throws Exception/用的缓冲方式文本读取 FileReader file=new FileReader(E:/txt1.txt); BufferedReader br=new BufferedReader(file); StringBuffer str=new StringBuffer(); String sw=br.readLine(); if(sw!=null) str.append(sw+ n); System.out.println(str); public void test5() throws Exception/用缓冲的方式写入数据然后再读入数据 FileWriter file=new FileWriter(E:/txt1.txt); BufferedWriter bw=new BufferedWriter(file); for(int i=1;i=10;i+) bw.write(这是第+i+行); bw.newLine(); bw.close();/写放数据 FileReader file1=new FileReader(E:/txt1.txt); BufferedReader br=new BufferedReader(file1); StringBuffer str=new StringBuffer(); String sw=br.readLine(); if(sw!=null) str.append(sw+ n); System.out.println(str); public void test6()/删除文件 File file=new File(E:/text.txt); if(file.exists() System.out.println(开始删除文件 :+file.getPath(); if(file.delete() System.out.println(文件删除成功); else System.out.println(文件删除失败); else System.out.println(该文件不存在); public void test7() throws Exception/创建 一个文件夹-创建一个目录或路径 File file=new File(E:/txt1.txt); if(file.exists()=false) file.createNewFile(); public void test8() throws Exception /* 将txt.txt复制文件到txt12.txt去;以字节流的的形式复制 File file=new File(E:/txt.txt); FileInputStream fi=new FileInputStream(file); byte content= new bytefi.available(); fi.read(content); fi.read(content, 0, content.length); String str=new String(content); System.out.println(str); File file1=new File(E:/txt12.txt); if(file.exists()=false) file.createNewFile(); /如果不存在该文件则创建一件文件 再行进复制 FileOutputStream fo=new FileOutputStream(file1,true); fo.write(content); fo.flush(); fo.close(); fi.close();/关闭流 public void test9()throws Exception /另和种方式复制文件 -从缓冲的形式复制 FileReader file=new FileReader(E:/txt.txt); BufferedReader br=new BufferedReader(file); String str=br.readLine(); /从文件 里面读取出来 FileWriter file1=new FileWriter(E:/txt1.txt); BufferedWriter bw=new BufferedWriter(file1); while(str!=null) bw.write(str); bw.newLine(); str = br.readLine(); bw.flush(); bw.close(); br.close(); public void test10() throws Exception File file=new File(E:/txt.txt); InputStream is = new FileInputStream(file); byte array = new byte3; int hasRead = 0; File file1=new File(E:/txt1.txt); OutputStream os = new FileOutputStream(file1); while(hasRead=is.read(array, 0, array.length)!=-1) System.out.println(读取了+hasRead+个字节); for(int i=0;iarray.length;i+) System.out.println(第+(i+1)+个:+arrayi)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论