java第十二周周总结

    xiaoxiao2024-11-16  55

    本周我们学习了有关文件即(io)的一些相关操作 InputStream is; //字节输入流 OutputStream os; //字节输出流 Reader reader; //字符输入流 Writer writer; //字符输出流 展示一些有关文件的一些类 都是java.io包里的 然后是 DataInputStream dis = new DataInputStream(System.in); //数据字节输入流 DataOutputStream dos = new DataOutputStream(System.out); // 数据字节输出流 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); /** * System.in :标准的字节输入流,代表的是键盘 * InputStereamReader: 将字节流转换成字符流 * BufferedReader:将字符流封装成缓冲字符输入流,提供行读取方法以提高输入效率 */

    下面是对文件的创建和删除的一些操作 我准备用案例来说明

    /** * 功能:创建目录与文件 * 获取目录或文件相关属性 * 删除目录或文件 * 日期:2019年5月18日 */ public class FileDemo01 { public static void main(String[] args) { // 创建File对象,指向一个目录,仅仅是一个逻辑概念 File dir = new File("d:/park01"); // 判断目录是否存在 if (!dir.exists()) { // 调用File对象的创建目录方法,将逻辑概念转换成物理实在 dir.mkdir(); // 提示用户目录创建成功 System.out.println("目录[" + dir.getPath() + "]创建成功!"); } else { // 提示用户目录已经存在 System.out.println("目录[" + dir.getPath() + "]已经存在!"); } 28 29 // 创建File对象,指向一个文件,仅仅是一个逻辑概念 30 File file = new File(dir, "love.txt"); 31 // 判断文件是否存在 32 if (!file.exists()) { 33 try { 34 // 调用File对象的创建文件方法,将逻辑概念转换成物理实在 35 file.createNewFile(); 36 // 提示用户文件创建成功 37 System.out.println("文件[" + file.getAbsolutePath() + "]创建成功!"); 38 } catch (IOException e) { 39 // 提示用户文件创建失败 40 System.out.println("文件[" + file.getAbsolutePath() + "]创建失败!"); 41 } 42 } else { 43 // 提示用户文件已经存在 44 System.out.println("文件[" + file.getAbsolutePath() + "]已经存在!"); 45 } 46 47 // 创建简单日期格式对象 48 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss"); 49 50 // 获取文件的相关属性 51 System.out.println("文件路径:" + file.getPath()); 52 System.out.println("文件绝对路径:" + file.getAbsolutePath()); 53 System.out.println("文件名:" + file.getName()); 54 System.out.println("文件长度:" + file.length()); 55 System.out.println("文件最后修改时间:" + sdf.format(file.lastModified())); 56 57 // 删除刚才创建的文件 58 boolean isFileDeleted = file.delete(); 59 // 判断文件是否删除成功 60 if (isFileDeleted) { 61 System.out.println("文件[" + file.getPath() + "]删除成功!"); 62 } else { 63 System.out.println("文件[" + file.getPath() + "]删除失败!"); 64 } 65 66 // 删除刚才创建的目录 67 boolean isDirDeleted = dir.delete(); 68 // 判断目录是否删除成功 69 if (isDirDeleted) { 70 System.out.println("目录[" + dir.getPath() + "]删除成功!"); 71 } else { 72 System.out.println("目录[" + dir.getPath() + "]删除失败!"); 73 } 74 } 75 }

    下面是对文件的读写的一些操作

    8 /** 9 * 功能:利用字节流完成文件读写操作 11 * 日期:2019年5月21日 12 */ 13 public class FileReadWrite01 { 14 public static void main(String[] args) { 15 // 创建File对象 16 File file = new File("love.txt"); 17 try { 18 // 创建文件 19 file.createNewFile(); 20 System.out.println("文件[" + file.getAbsolutePath() + "]创建成功!"); 21 // 创建文件字节输出流 22 FileOutputStream fos = new FileOutputStream(file); 23 // 往文件里写入字节数据(一次一个字节) 24 fos.write('I'); 25 fos.write(' '); 26 fos.write('l'); 27 fos.write('o'); 28 fos.write('v'); 29 fos.write('e'); 30 fos.write(' '); 31 fos.write('y'); 32 fos.write('o'); 33 fos.write('u'); 34 fos.write('!'); 35 // 往文件里写入字节数据(一次一个字节数组) 36 fos.write("\nDo you love me?".getBytes()); 37 // 往文件里写入字节数据(一次一个字节数组的部分) 38 fos.write('\n'); 39 fos.write("I don't love you.".getBytes(), 8,8); 40 // 提示用户文件写入成功 41 System.out.println("恭喜,文件写入成功!"); 42 43 // 创建文件字节输入流 44 FileInputStream fis = new FileInputStream(file); 45 // 方法一、采用for循环读取文件内容 46 int length = fis.available(); // 获取文件字节流中数据长度 47 for (int i = 0; i < length; i++) { 48 System.out.print((char) fis.read()); 49 } 50 System.out.println(); 51 52 // 方法二、采用while循环读取文件内容(逐个字节读取) 53 fis = new FileInputStream(file); 54 int i; 55 while ((i = fis.read()) != -1) { 56 System.out.print((char) i); 57 } 58 System.out.println(); 59 60 // 方法三、采用while循环读取文件内容(逐个字节数组读取) 61 fis = new FileInputStream(file); 62 byte[] b = new byte[6]; // 每次读取6个字节 63 while (fis.read(b) != -1) { 64 for (int k = 0; k < b.length; k++) { 65 System.out.print((char) b[k]); 66 } 67 } 68 } catch (IOException e) { 69 System.err.println("文件[" + file.getAbsolutePath() + "]创建失败!"); 70 } 71 } 72 }

    这里面主要用用到的类有: FileOutputStream fos = new FileOutputStream(file); **// 创建文件字节输出流 ** FileInputStream fis = new FileInputStream(file); 创建文件字节输入流 方法有:write(),read ()分别对应读写

    下面这个是本周最后的内容 这里面又有新的类 RandomAccessFile,第一个 参数为文件名,第二个参数为读写方法:r为读,w为写。 其实这不是一周学的内容,而是几周的内容,我不分开写的原因是:这样更能形成一种逻辑图在脑子里

    在下周的学习中可能还有点文件操作的相关的问题,我会加到这里一起写的。下周的学习中我也将更加的努力。

    最新回复(0)