关于txt及其他文件的基本操作,创建、读写、复制、删除(菜鸟的笔记)

1.文件的创建

String mailBodypath = configPath +"/mail"+".txt";

String sendTopath = configPath +"/To"+".properties";

根据传入的不同,可以生成不同格式的文件。

private static void CreateTxt(String dir)

        {
            try {
                File dirPath = new File(dir);
                if (!dirPath.exists()) {
                    dirPath.createNewFile();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

2.文件的复制

 public static void copyFile(String oldPath, String newPath) {
         try {
             int bytesum = 0;
             int byteread = 0;
             File oldfile = new File(oldPath);
             if (oldfile.exists()) { //文件存在时
                 InputStream inStream = new FileInputStream(oldPath); //读入原文件
                 FileOutputStream fs = new FileOutputStream(newPath);
                 byte[] buffer = new byte[1444];
                 while ( (byteread = inStream.read(buffer)) != -1) {
                     bytesum += byteread; //字节数 文件大小
                     fs.write(buffer, 0, byteread);
                 }
                 inStream.close();
                 fs.close();
             }
         }
         catch (Exception e) {
             e.printStackTrace();
         }

     }

3.文件的写入

public static void contentToSendTo(String filePath,String inf1 , String inf2) {
            try{
                BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true));
                writer.write(inf1);       //写入第一行数据
                writer.write("\r\n");    //换行
                writer.write(inf2);       //写入第二行数据
                writer.write("\r\n");    //换行
                writer.close();
            }catch(Exception e){
                e.printStackTrace();
            }

        }

4.文档的读取

public static void readFile(String filePath){
        try {
                String encoding="utf-8";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){
                    InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),encoding);
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null)                      
                    {
                        System.out.println("\""+ lineTxt + "\",");
                    }
                    read.close();
        }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

5.文档的删除

    public void deleteExcel(File file){
            try{
                if (file.isFile()) {
                    file.delete();
                }
            }
            catch(NullPointerException n){
                System.out.println("Sorry,No such file");
            }

    }

6.生成.ini文件

 public  void writeINI(String name) throws IOException {
             StringBuilder sb = new StringBuilder("");
             for (String section : iniFile.keySet()) {
                 sb.append("[").append(section).append("]").append("\n");
                 Map<String, Object> map = iniFile.get(section);
                 Set<String> keySet = map.keySet();
                 for (String key : keySet) {
                     sb.append(key).append("=").append(map.get(key)).append("\n");
                 }
             }
             File file = new File(name);
             if (!file.exists()) {
                 file.createNewFile();
             }
             else{
                 return;
             }
             try {
                 OutputStream os = new FileOutputStream(file);
                 os.write(sb.toString().getBytes());
                 os.flush();
                 os.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值