java IO流例子详解_免费下载.doc_第1页
java IO流例子详解_免费下载.doc_第2页
java IO流例子详解_免费下载.doc_第3页
java IO流例子详解_免费下载.doc_第4页
java IO流例子详解_免费下载.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

广州java培训 1 只显示关键字的文件或文件夹1.1 要实现一个文件过滤接口FileFilter,重写其中的public boolean accept(File pathname);如:public class KeywordFileFilter implements FileFilter private String keyword;public KeywordFileFilter(String keyword) this.keyword = keyword;public boolean accept(File pathname) return pathname.getName().toLowerCase().indexOf(keyword) = 0;2 File使用只显示有java的文件或文件夹:KeywordFileFilter keywordFileFilter=new KeywordFileFilter(java);File files = file.listFiles(keywordFileFilter);File file = new File(E:xindongfangCoreJava);/ 文件是否存在if (file.exists() System.out.println(是否为文件夹: + file.isDirectory();System.out.println(是否为文件: + file.isFile();System.out.println(是否隐藏: + file.isHidden();System.out.println(是否可读: + file.canRead();System.out.println(是否可写: + file.canWrite();System.out.println(最后修改时间:: + new Date(file.lastModified();System.out.println(文件的大小: + file.length();File file_new = new File(E:xindongfangCoreJavaaabb);/如果存在则不创建/System.out.println(创建文件: + file_new.createNewFile();/只创建文件夹,如果文件中的路径包含有未创建的文件,则不会创建/System.out.println(创建文件夹: + file_new.mkdir();/只创建文件夹,包含文件夹的层次关系,存在则不创建/System.out.println(创建文件夹(包括所有必需但不存在的父目录): + file_new.mkdirs();/删除文件,如果删除文件夹则要先要删除文件夹中的文件,再后才删除空的文件夹System.out.println(删除文件: + file_new.delete();/ KeywordFileFilter keywordFileFilter=news/ KeywordFileFilter(java);/ File files = file.listFiles();/ System.out.println(文件中的数量: + files.length);/ for (int i = 0; i files.length; i+) / System.out.println(文件中文件名称: + filesi.getName() + ;是否隐藏:/ + filesi.isHidden() + ;文件绝对路径:/ + filesi.getAbsolutePath() + ;上级文件/ + filesi.getParent()+;最后修改时间:+filesi.lastModified();/ 3 复制文件二进制,InputStream/OutputStream在读取文件时,通过循环来读取,要先将read赋值给一个临时变量,再判断这个变量是否=-1,如int temp=0;while (temp=inputStm.read() != -1) System.out.println(temp);如果写成:while (inputStm.read() != -1) System.out.println(inputStm.read();这样输出的内容不全,如果写到一个新的文件中,则此文件的内容和原文件的内容来不一样,在关闭资源时,要清空下输出流,在读取文件时,是转换成int 类型的,如果最后的数据不足32位时,所以要强制清空下复制文件:public static void copyFile(String sourceFile,String destFile) try InputStream inputStm = new FileInputStream(sourceFile);OutputStream outputStm=new FileOutputStream(destFile);int temp=0;while (temp=inputStm.read() != -1) outputStm.write(temp);inputStm.close();outputStm.flush();outputStm.close();System.out.println(复制OK); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();4 输出流二进制OutputStream写文件OutputStream outputStm = new FileOutputStream(file,true);第二个参数为true,表示会在文件的末尾追加内容,为false表示会覆盖原文件的内容public static void writeByOutStream(String filePath) File file = new File(filePath);try if (!file.exists() file.createNewFile();OutputStream outputStm = new FileOutputStream(file,false);String str=您好!我用OutputStream来写文件的内容!;outputStm.write(str.getBytes(); str=添加内容!;outputStm.write(str.getBytes();outputStm.flush();outputStm.close();System.out.println(写入成功!); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();5 复制文件字符流 FileReader FileWriterpublic static void copyFileByReader(String sourceFile, String destFile) try FileReader fileRead = new FileReader(sourceFile);FileWriter fileWrit = new FileWriter(destFile);int temp = 0;while (temp = fileRead.read() != -1) / System.out.println(inputStm.read();fileWrit.write(temp);fileRead.close();fileWrit.flush();fileWrit.write(rn);fileWrit.write(fdsfa在在地sd);fileWrit.close();System.out.println(复制OK); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();6 用字符流BufferedReader读取文件/用FileReader读取文件public static void readByFileReader(String filePath) File file = new File(filePath);if (!file.exists() System.out.println(文件不存在 + filePath);return;try FileReader fileReader = new FileReader(file);BufferedReader bufferedReader = new BufferedReader(fileReader);String temp = null;while (temp = bufferedReader.readLine() != null) System.out.println(temp);/以下方式读取会读不尽文件的内容/ while ( bufferedReader.readLine() != null) / System.out.println(bufferedReader.readLine();/ bufferedReader.close();fileReader.close();System.out.println(BufferedReader读文件完毕 !); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();7 字符流BufferedWriter写文件/ 利用BufferedWriter的方法写文本文件private static void writeByWriter(String filePath) File file = new File(filePath);try if (!file.exists() / System.out.println(文件不存在 + filePath);file.createNewFile();FileWriter fileWriter;fileWriter = new FileWriter(file,false);BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);bufferedWriter.write(我用BufferedWriter写文件);bufferedWriter.close();fileWriter.close();System.out.println(写入成功!); catch (IOException e) e.printStackTrace();8 用DataOutputStream写文件/ 用DataOutputStream写文件private static void writeByDataOutput(String filePath) try DataOutputStream daOutputStream = new DataOutputStream(new FileOutputStream(filePath);/ 要写入的内容String name = aa, 张三, 李白, 杜甫, 不知道 ;/ 姓名double weight = 110.2, 80.8, 90.4, 92.3, 70.8 ;/ 体重int height = 175, 170, 169, 160, 159 ;/ 身高/ 循环写入,格式:name weight height换行for (int i = 0; i + name);System.out.println(weight + weight);System.out.println(height + height); catch (IOException e) / 当文本被全部读出以后会抛出EOFException例外,中断循环System.out.println(文件读取完毕);try if (daInputStream != null) daInputStream.close(); catch (IOException e) e.printStackTrace();10 RandomAccessFile 读写文件/ RandomAccessFile 读写文件private static void readOrWriteByRandom(String filePath) File file = new File(filePath);RandomAccessFile ranAccessFile = null;try /if (!file.exists() file.createNewFile();/ 创建一个随机读取对象ranAccessFile = new RandomAccessFile(file, rw);System.out.println(写入前文件的长度: + (ranAccessFile.length() + B);/去到文件的末尾ranAccessFile.seek(ranAccessFile.length();/ 开始写入内容ranAccessFile.writeBoolean(true);ranAccessFile.writeChars(RandomAccessFile);ranAccessFile.writeChar(n);/ 加个换行符ranAccessFile.writeDouble(23.456);ranAccessFile.writeBoolean(false);System.out.println(写入后文件的长度: + (ranAccessFile.length() + B);/ 在读取前要先说明是从哪个位置开始读取,否则有异常/ranAccessFile.seek(0);int beginIndex = (ranAccessFile.length() 50) ? 50 : 0; / 将读文件的开始位置移到beginIndex位置。ranAc

温馨提示

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

评论

0/150

提交评论