已阅读5页,还剩31页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
package pk.m11.d14.a1;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class CopyFileTest /* * 、将FileInputStream和FileOutputStream类配合实现文件的复制。 * * 结论: 文件的字节可以实现任意文件的处理(如: 复制任意文件) */public static void main(String args) /1 准备要复制的文件和要生成的文件名。String path1 = d:2015北京地铁规划图.bmp;String path2 = f:new2015北京地铁规划图.bmp;/2 声明FileInputStream fis = null; /用来读取文件FileOutputStream fos = null; /用来存盘文件。System.out.println(正在复制文件,请等待.);/3 创建try fis = new FileInputStream( path1 ); /用来读fos = new FileOutputStream( path2 ); /用来写/4 一边读一边写int x = fis.read();while( x != -1 ) /5 判断fos.write( x ); /6 存盘x = fis.read(); /7 继续读取/8 确保存盘成功fos.flush(); System.out.println(n已将 + path1 + 复制到 + path2 + 中了。); catch (FileNotFoundException e) catch (IOException e) finally /9 关闭try if( fis != null )fis.close(); catch (IOException e1) try if( fos != null ) fos.close(); catch (IOException e) package pk.m11.d14.a1;public class ReaderWriterTest /* * 1、 Reader抽象类的特性: * 1) 它以字符的方式来读取数据。 * * 2) 它提供的常用方法: * int read(); 用来读取一个字符,反馈该字符的编码值,即:一个int型整数,其范围为0 , 65535;但当它反馈一个-1时表明结束了。 * * void close(); 用来关闭字符输入流,释放所占资源,确保安全。 * * 它的常用类: FileReader 文件的字符输入流。 * 通过案例来理解并掌握 FileReader类的应用。 * * 2、 Writer 抽象类的特性: * 1) 它以字符的方式将给定的数据输出。当给定的数据超出了0,65535范围时,则会失真。 * * 2) 它提供的常用方法: * void write( int x ); 用来将给定的整数以字符方式输出。 * * void flush(); 将缓冲区中的数据一次性输出,以确保输出成功。 * * void close(); 用来关闭字符输出流,释放所占资源,确保安全。 * * void write( String str ); 用来将给定的字符串输出。 * * 它的常用类: FileWriter 文件的字符输出流。 * 通过案例来理解并掌握 FileWriter 类的应用。 */package pk.m11.d14.a1;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class FileReaderTest /* * 通过FileReader 文件的字符输入流来读取文件的内容。 */public static void main(String args) /1 准备要读取的文件String path = d:/a1.java;/2 声明FileReader fr = null;/3 创建try fr = new FileReader( path );/4 读取int x = fr.read(); /读取一个字符while( x != -1 ) /5 判断System.out.print( (char)x ); /6 显示x = fr.read();/7 继续读取System.out.println(n读取 + path + 文件内容完毕。); catch (FileNotFoundException e) catch (IOException e) finally /8 关闭try fr.close(); catch (IOException e) package pk.m11.d14.a1;import java.io.FileWriter;import java.io.IOException;import java.util.Arrays;public class FileWriterTest /* * 通过FileWriter文件的字符输出流类实现数据存盘。 * */public static void main(String args) /1 准备要存盘的数据和文件String path = f:chars.txt;char hzs = 欢,迎,你,们,来,到,北,京,!;/2 声明FileWriter fw = null;/3 创建try fw = new FileWriter( path );/4 存盘for(char ch = A; ch = Z; ch+)fw.write( ch );fw.write( ch+32 );fw.write(rn); for(int i = 0; i hzs.length; i+)fw.write( hzsi );/5 确保存盘成功fw.flush();System.out.println(已将26个大小写字母及 + Arrays.toString(hzs) + 存盘到 + path + 文件中了。); catch (IOException e) finally /6 关闭try fw.close(); catch (IOException e) package pk.m11.d14.a1;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FileCopyTest_RW /* * 将 FileReader 和 FileWriter 文件的字符输入输出流配合实现文件的复制。 * * 结论: 它们俩者的结合只能处理文本文件,而不用来处理特殊格式的文件 * (如: 图片,*.doc, *.ppt 等) * * 倒型树结构来管理计算机的资源. */public static void main(String args) /1 准备要复制的文件和存盘的文件名String path1 = d:2015北京地铁规划图.bmp;String path2 = f:new2015北京地铁规划图.bmp;System.out.println(正在复制文件,请等待.);/2 声明FileReader fr = null;FileWriter fw = null;/3 创建try fr = new FileReader( path1 );fw = new FileWriter( path2 );/4 一边读一边写int x = fr.read();while( x != -1 ) /5 判断fw.write( x );/6 存盘x = fr.read(); /7 继续读取/8 确保存盘fw.flush(); System.out.println(n已将 + path1 + 复制到 + path2 + 中了.); catch (FileNotFoundException e) catch (IOException e) finally try fr.close(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace();try fw.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();package pk.m11.d14.a1;import java.io.File;import java.util.Date;public class FileTest1 /* * 1、java语言中,来自java.io包中的 File类。 * 1) 它用来管理计算机的资源(主要指: 磁盘、文件、文件夹)。 * 即: 查看磁盘空间使用情况,创建、删除、修改文件夹或文件等操作。 * * 2) 它提供了一些常用的方法,用来管理文件夹或文件等资源。 * * . * * 3) 通过案例理解并掌握 File类的基本应用。 * a) 查看指定文件夹(目录)下的信息。 */public static void main(String args) System.out.println(当前用户的工作位置是: + System.getProperty(user.dir);String path = System.getProperty(user.dir);/创建一个File类的对象用来管理path路径File f1 = new File( path );String dirFileNames = f1.list(); /将指定目录下的子目录名和文件名收集起来构成一个字符串数组。for(String tem : dirFileNames )File f2 = new File( path + + tem );System.out.print( tem + t );if( f2.isDirectory() )System.out.print(tt文件夹t);elselong len = f2.length(); System.out.print( len + t文件t);long time1 = f2.lastModified();Date d1 = new Date( time1 );System.out.printf(%tF %tT n , d1);package pk.m11.d14.a2;import java.io.File;import java.io.IOException;public class FileTest2 /创建一系列目录和一个文件。public static void main(String args) /获取用户当前工作目录(工作位置)String path = System.getProperty( user.dir );String dirs = 目录1 + + 目录11;String fileName = MyFile.txt;File f1 = new File(d:+dirs, fileName );if( f1.exists() )System.out.println(获取 + fileName + 文件的绝对路径为: + f1.getAbsolutePath() );System.out.println(获取 + fileName + 文件大小为: + f1.length() + 字节。 );elsef1.getParentFile().mkdirs(); System.out.println(n在当前工作位置下创建了一系列目录: + dirs );try f1.createNewFile();System.out.println(n在 + dirs + 位置下创建了 + fileName + 文件。); catch (IOException e) package pk.m11.d14.a2;public class BufferStreamTest /* * 1、在java语言中,提供了 BufferedInputStream 缓冲的字节输入流。 * 它用来提高输入的速度; 它属于处理流;因此,它必须套接在节点流上。 * 如: 通常它套接在 FileInputStream 节点流上,以便提高读取文件的速度。 * * 2、在java语言中,提供了 BufferedOutputStream 缓冲的字节输出流。 * 它用来提高输出的速度;它属于处理流;因此,它必须套接在节点流上。 * 如: 通常它套接在 FileOutputStream 节点流上,以便提高存盘的速度。 * * 注意: 以上两者结合可提高文件复制的速度和效率。 * * 3、在java语言中,也提供了 BufferedReader 缓冲的字符输入流。 * 它用来提高输入的速度; 它属于处理流;因此,它往往需要套接在节点流上。 * 如: 通常它套接在 FileReader 节点流上,以便提高读取字符的速度。 * * 4、在java语言中,也提供了 BufferedWriter 缓冲的字符输出流。 * 它用来提高输出的速度;它属于处理流;因此,它往往需要套接在节点流上。 * 如: 通常它套接在 FileWriter 节点流上,以便提高字符存盘的速度。 * * 5、缓冲流的个性: * mark()方法用来作标记。 * reset() 方法用来返回到标记处。 * * 6、BufferedReader缓冲的字符输入流提供的个性方法: * String readLine()方法 用来读取一行内容。 * * 7、BufferedWriter 缓冲的字符输出流提供的个性方法: * void write(String str)方法用来输出一个字符串。 * void newLine()方法用来输出一个换行。 * */package pk.m11.d14.a2;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileCopyTest_New /* * 如何提高文件的复制速度? * 答案: 使用缓冲流。 验证知识点1、2 */public static void main(String args) /1 准备String path1 = d:2015北京地铁规划图.bmp;String path2 = f:new2015北京地铁规划图.bmp;System.out.println(正在复制文件,请等待.);/2 声明FileInputStream fis = null;FileOutputStream fos = null;/BufferedInputStream bis = null;BufferedOutputStream bos = null;/3 创建try fis = new FileInputStream( path1 );fos = new FileOutputStream( path2 );/bis = new BufferedInputStream( fis ); /以便提高读取的速度bos = new BufferedOutputStream( fos ); /以便提高存盘的速度/long t1 = System.currentTimeMillis();/4 一边读取一边存盘int x = bis.read(); /fis.read();while( x != -1 )/5 判断bos.write( x ); /fos.write( x );x = bis.read(); /fis.read();/确保存盘成功bos.flush(); /fos.flush();long t2 = System.currentTimeMillis();System.out.println(n文件复制完毕,所消耗的时间为: + (t2-t1) + 毫秒。); catch (FileNotFoundException e) catch (IOException e) finally try bis.close(); catch (IOException e2) try fis.close(); catch (IOException e1) try bos.close(); catch (IOException e1) try fos.close(); catch (IOException e) 验证 5、缓冲流的个性: * mark()方法用来作标记。 * reset() 方法用来返回到标记处。package pk.m11.d14.a2;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class MarkResetTest public static void main(String args) /1 准备String path = F:hello.txt;/2 声明BufferedInputStream bis = null;/3 创建try bis = new BufferedInputStream( new FileInputStream(path) );int x = bis.read();System.out.println( (char)x );x = bis.read();System.out.println( (char)x );bis.mark(156); /作一个标记x = bis.read();for(int i = 1; i = 10 & x != -1 ; i+)System.out.print( (char)x );x = bis.read();bis.reset(); /返回到标记处x = bis.read();for(int i = 1; i = 10 & x != -1 ; i+)System.out.print( (char)x );x = bis.read(); catch (FileNotFoundException e) catch (IOException e) finally try bis.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();验证 6、BufferedReader缓冲的字符输入流提供的个性方法: * String readLine()方法 用来读取一行内容。 * * 7、BufferedWriter 缓冲的字符输出流提供的个性方法: * void write(String str)方法用来输出一个字符串。 * void newLine()方法用来输出一个换行。package pk.m11.d14.a2;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FileCopyTest_RW_New public static void main(String args) /1 准备String path1 = d:a1.java;String path2 = f:newa1.java;/2 声明BufferedReader br = null;BufferedWriter bw = null;/3 创建try br = new BufferedReader( new FileReader(path1) );bw = new BufferedWriter( new FileWriter(path2) );/4 一边读取一边存盘String str = br.readLine(); /读取一行while( str != null ) /5 判断bw.write( str ); /6存盘/bw.write( rn );bw.newLine(); /产生一个新行,即:换行 str = br.readLine();/7 继续读取/8确保存盘成功bw.flush();System.out.println(n复制完毕。); catch (FileNotFoundException e) catch (IOException e) finally try br.close(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace();try bw.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();18.30 晚package pk.m11.d14.a3;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.LineNumberReader;public class LineNumberReaderTest /* * 1、LineNumberReader类,它以行的方式读取文件的内容。它属于处理流, * 需要套接在节点流上;通常套接在 FileReader文件的字符输入流上。 * * getLineNumber();用来读取行号 * readLine();用来读取一行内容。 * */以行的方式读取文件的内容。public static void main(String args) String path = d:a1.java;LineNumberReader lnr = null;try lnr = new LineNumberReader( new FileReader(path) );int row = lnr.getLineNumber(); /读取行号String str = lnr.readLine(); /读取一行while( str != null )System.out.printf( %2d: %s n , row+1, str );row = lnr.getLineNumber();str = lnr.readLine();System.out.println(n读取 + path + 文件内容完毕。); catch (FileNotFoundException e) catch (IOException e) finally try lnr.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();package pk.m11.d14.a3;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.Arrays;public class InputStreamReaderOuputStreamWriterTest /* * 1、java语言中的转换流: * 1) InputStreamReader 字节转换成字符的输入流,以字符方式读取数据。 * * 2) OutputStreamWriter 字符转换成字节的输出流,以字节方式输出数据。 * * 给的是字节流,要的是字符流; * 给的是字符流,要的是字节流。 * * 注意: 它们属于处理流,往往需要套接在节点流上。 * 如: InputStreamReader ,它往往套接在 FileInputStream节点流。 * * 如: OutputStreamWriter,它往往套接在 FileOutputStream 节点流。 * * 案例1: FileInputStream节点流在读取带汉字的文件时,显示会失真。 * 通过InputStreamReader 转换来解决此问题。 * * 案例2: FileOutputStream节点流在存盘汉字时会失真。 * 通过 OutputStreamWriter 转换来解决此问题。 * * 2、转换流的特有功能: * 在创建转换流对象时,可以指定字符编码集方案。 * * 即:以指定的字符编码方案实现数据的存盘或读取。 * * 案例1: 以指定的字符编码方案实现数据的存盘。 * * 3、在java5.0之前通常使用流技术实现控
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年信息安全风险评估与实施考试卷
- 2026年天津市北师大版高一数学必修第2章复习题
- 2025-2026学年大班荷花说课稿
- 2025-2026学年大苹果美术说课稿
- 2025-2026学年分工合作幼儿说课稿
- 2025-2026学年宠物写生说课稿
- 2025年湖北省武穴市高二生物下册期末考试模拟测试卷及参考答案(B卷)
- 2025-2026学年初一语文春说课稿
- 江苏事业单位计算机岗招聘笔试专项训练题库及答案
- 2025-2026学年大班树叶说课稿的科学
- 2026云南曲靖市水务投资限公司招聘工程专业技术人员(第77期)易考易错模拟试题(共500题)试卷后附参考答案
- 设备点检员安全综合水平考核试卷含答案
- 2026年中秋国庆节前安全专题培训(危化化工版)
- 2026国家会展中心(天津)有限责任公司人员招聘9人笔试备考试题及答案详解
- 制造业数字化转型2026年培训课件
- 科粤版九年级化学上册第二单元《空气、物质的构成与组成》单元检测卷(含答案)
- 儿童淋巴结肿大诊治共识
- 甘肃省医保政策培训课件
- 舞台灯光调试与安装施工方案
- (正式版)DB65∕T 4733-2023 《石化行业雷电灾害隐患排查指南》
- 学堂在线 庄子哲学导读 章节测试答案
评论
0/150
提交评论