本文主要介绍 Java IO 中几个常见的操作,读取字符,写入字符,复制文件,复制文件夹等。
直接上代码啦
因为读和写比较常用,尤其是按行读取,所以贴前面啦
其他的如下
上面的代码只介绍了一些基本的简单的操作,以后遇到其他的再补充吧。最后再上一张结构图吧
                    直接上代码啦
因为读和写比较常用,尤其是按行读取,所以贴前面啦
- /**
 - * 从文件里读取内容
 - *
 - * @param filePath
 - * @return
 - */
 - private static String readFromFile(String filePath) {
 - // 使用ArrayList来存储每行读取到的字符串
 - StringBuilder sb = new StringBuilder();
 - try {
 - FileReader fr = new FileReader(filePath);
 - BufferedReader bf = new BufferedReader(fr);
 - String str;
 - // 按行读取字符串
 - while ((str = bf.readLine()) != null) {
 - sb.append(str + "\r\n");
 - }
 - bf.close();
 - fr.close();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - return sb.toString();
 - }
 - /**
 - * 往文件中写入内容
 - *
 - * @param outputFilePath 输出文件路径
 - * @param content
 - */
 - public static void writeToFile(String outputFilePath, String content) {
 - File file = new File(outputFilePath);
 - try {
 - FileWriter writer = new FileWriter(file);
 - writer.write(content);
 - writer.close();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 
其他的如下
- package com.liuyanzhao.file;
 - import java.io.*;
 - /**
 - * @author 言曌
 - * @date 2017/10/21 下午3:31
 - */
 - public class Demo {
 - /**
 - * 创建文件
 - * @param file 文件
 - */
 - public static void createFile(File file) {
 - try {
 - file.createNewFile();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 - /**
 - * 删除文件
 - * @param file 文件
 - */
 - public static void deleteFile(File file) {
 - file.delete();
 - }
 - /**
 - * 根据字节流读取文件内容
 - * @param file 文件
 - */
 - public static void readFileByInputStream(File file) {
 - try {
 - FileInputStream fis = new FileInputStream(file);
 - int length;
 - byte[] buf = new byte[1024];
 - while ((length = fis.read(buf)) != -1) {
 - System.out.print(new String(buf, 0, length));
 - }
 - fis.close();
 - } catch (FileNotFoundException e) {
 - e.printStackTrace();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 - /**
 - * 根据字符流读取文件内容
 - *
 - * @param file 文件
 - */
 - public static void readFileByReader(File file) {
 - try {
 - FileReader reader = new FileReader(file);
 - char[] buf = new char[1024];
 - int length;
 - while ((length = reader.read(buf)) != -1) {
 - System.out.print(new String(buf, 0, length));
 - }
 - } catch (FileNotFoundException e) {
 - e.printStackTrace();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 - /**
 - * 往文件中写入内容(直接覆盖)
 - * @param file 文件
 - * @param content
 - */
 - public static void writeByWriter(File file, String content) {
 - try {
 - FileWriter writer = new FileWriter(file);
 - writer.write(content);
 - writer.close();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 - /**
 - * 往文件中写入内容(直接覆盖)
 - * @param file
 - * @param content
 - */
 - public static void writeByOutputStream(File file, String content) {
 - try {
 - FileOutputStream fos = new FileOutputStream(file);
 - fos.write(content.getBytes());
 - fos.close();
 - } catch (FileNotFoundException e) {
 - e.printStackTrace();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 - public static void main(String args[]) {
 - // File file = new File("/Users/liuyanzhao/Desktop/demo.txt");
 - //// readFileByInputStream(file);
 - //// readFileByReader(file);
 - //// writeByOutputStream(file,"hello saysky");
 - //// writeByWriter(file,"hello java");
 - File file = new File("/Users/liuyanzhao/Desktop");
 - for(File file1: file.listFiles()) {
 - System.out.println("名称:"+file1.getName());
 - System.out.println("是否存在:"+file1.exists());
 - System.out.println("绝对路径:"+file1.getAbsolutePath());
 - System.out.println("父级目录:"+file1.getParent());
 - System.out.println("大小:"+file1.length()/1000+"kB");
 - }
 - }
 - }
 
上面的代码只介绍了一些基本的简单的操作,以后遇到其他的再补充吧。最后再上一张结构图吧

                            
                                


                
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏