Java简单的IO流文件操作(复制、写入、读取)

这是一个关于Java代码的示例,展示了如何将代码文件转换成Word文档并进行文件操作,如遍历目录、读写文件内容。程序遍历指定目录下的所有文件,将文件内容追加到一个Word文档中,并跳过注释行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例:

package com.example.rfos_common_handler.demo;

import java.io.*;

/**
 * @Author rfos
 * @Date 2023/2/22 21:13
 * @Description TODO 代码写软著
 */

public class CodeToWord1 {
    private static String filePath = "F:\\Tian文档\\软件著作\\红火蚁\\views";
    private static String toPath = "F:\\Tian文档\\软件著作\\红火蚁\\views.docx";
    private static Integer i =0;
    private static Integer j =0;
    public static void main(String args[]) throws IOException {
        editToWord(filePath);
        System.out.println("总行数:"+j);
    }
    public static String editToWord(String filePath ) throws IOException {
        File file = new File(filePath);
        File[] files = file.listFiles();//遍历path下的文件和目录,放在File数组中

        for (File file1 : files) {
            if(file1.isDirectory()){
                filePath = file1.toString();
                editToWord(filePath);
            }else {
                System.out.println(file1.getName());
                System.out.println(++i);
                //文件名
                String filename = file1.getName();
                //读文件
                FileInputStream fileInputStream = new FileInputStream(file1);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                //追加的方式+true

                FileOutputStream fileOutputStream = new FileOutputStream(toPath,true);
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));

                String content;
                while ( (content = bufferedReader.readLine()) != null){
//                    String s = new String(read, 0, length);
                    System.out.println((++j) +":"+content);
                    if (content.startsWith("//")){
                        System.out.println(true);
                        continue;
                    }
//                    byte[] read = content.getBytes();
//                    fileOutputStream.write(read);
                    j++;
                    bufferedWriter.write(content);
                    //换行
                    bufferedWriter.newLine();

                }
                bufferedReader.close();
                fileInputStream.close();
//                bufferedWriter.flush();
                fileOutputStream.close();
//                bufferedWriter.close();

            }
        }
        return null;
    }
}



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * IO流
 */
public class IOUtils {
    private static String path="F://LearningDirection/JavaSE/javase/readhello.txt";
    private static String path1="F://LearningDirection/JavaSE/javase/readhello1.txt";
    /**
     * FileInputStream类的使用:读取文件内容
     */
    public static String readFileContent() {
        IOUtils ioUtils = new IOUtils();
        //电脑d盘中的abc.txt 文档
        String filePath = path;
        String reslut = ioUtils.readFile( filePath ) ;
        System.out.println( reslut );
        return reslut;
    }



    /**
     * 读取指定文件的内容
     * @param filePath : 文件的路径
     * @return  返回的结果
     */
    public String readFile( String filePath ){
        FileInputStream fis=null;
        String result = "" ;
        try {
            // 根据path路径实例化一个输入流的对象
            fis  = new FileInputStream( filePath );

            //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
            int size =  fis.available() ;
            //3. 根据输入流中的字节数创建byte数组;
            byte[] array = new byte[size];
            //4.把数据读取到数组中;
            fis.read( array ) ;

            //5.根据获取到的Byte数组新建一个字符串,然后输出;
            result = new String(array);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if ( fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return result ;
    }
    /**
     * FileOutputStream 类的使用:将内容写入文件
     */
    public static void writeContentToFile(String[] args) {
        IOUtils a2 = new IOUtils();

        //电脑d盘中的abc.txt 文档
        String filePath = path;

        //要写入的内容
        String content = "今天是2017/1/9,天气很好" ;
        a2.writeFile( filePath , content  ) ;
    }
    /**
     * 根据文件路径创建输出流
     * @param filePath : 文件的路径
     * @param content : 需要写入的内容
     */
    public void writeFile( String filePath , String content ){
        FileOutputStream fos = null ;
        try {
            //1、根据文件路径创建输出流
            fos  = new FileOutputStream( filePath );

            //2、把string转换为byte数组;
            byte[] array = content.getBytes() ;
            //3、把byte数组输出;
            fos.write( array );

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if ( fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * ,实现复制文件,从D盘复制到E盘
     */
    public static void main(String[] args) {
        IOUtils a2 = new IOUtils();

        //电脑d盘中的cat.png 图片的路径
        String filePath1 = path;

        //电脑e盘中的cat.png 图片的路径
        String filePath2 = path1;

        //复制文件
        a2.copyFile( filePath1 , filePath2 );

    }

    /**
     * 文件复制
     * @param filePath_old : 需要复制文件的路径
     * @param filePath_new : 复制文件存放的路径
     */
    public void copyFile( String filePath_old  , String filePath_new){
        FileInputStream fis=null ;
        FileOutputStream fout = null ;
        try {
            // 根据path路径实例化一个输入流的对象
            fis  = new FileInputStream( filePath_old );

            //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
            int size =  fis.available() ;
            //3. 根据输入流中的字节数创建byte数组;
            byte[] array = new byte[size];
            //4.把数据读取到数组中;
            fis.read( array ) ;

            //5、根据文件路径创建输出流
            fout = new FileOutputStream( filePath_new ) ;

            //5、把byte数组输出;
            fout.write( array );

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if ( fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if ( fout != null ) {
                try {
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锐行织梦者

谢谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值