Java,将一个文件分若干组另存为的方法

package com.itrun.ssm.domain;

import java.io.*;

public class test {
    public static void main(String args[]) {
        try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw

            /* 读入TXT文件 */
            String pathname = "F:\\readAndWrite\\2-200.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
            File filename = new File(pathname); // 要读取以上路径的input。txt文件
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(filename)); // 建立一个输入流对象reader
            BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
            String line1 = "";
            String line2 = "";
            String str1 = "";
            String str2 = "";
            int l1 ;
            int l2 ;
            line1 = br.readLine();
            line2 = line1;
            int index = 100;
            File subFile = new File("F://readAndWrite//result//" + index + ".txt");
            if (!subFile.exists()) {
                subFile.createNewFile();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(subFile, true));
            while (line2 != null) {
                line1 = br.readLine();
                if (line1 == null){
                    out.write(line2 + "\r\n");
                    //out.write(line2 + "\r\n");
                    out.close();
                    break;
                }
                str1 = line1.substring(line1.length()-1,line1.length());
                str2 = line2.substring(line2.length()-1,line2.length());
                l1 = Integer.parseInt(str1);
                l2 = Integer.parseInt(str2);

                if (l1 < l2 ) {
                    out.write(line2 + "\r\n");
                    line2 = line1;
                    out.close();
                    index += 1;
                    subFile = new File("F://readAndWrite//result//" + index + ".txt");
                    if (!subFile.exists()) {
                        subFile.createNewFile();
                    }
                    out = new BufferedWriter(new FileWriter(subFile));
                    //out.write(line1 + "\r\n"); // \r\n即为换行
                    out.write(line1 + "\r\n");
                    /*out.write(line1); // \r\n即为换行
                    out.flush(); // 把缓存区内容压入文件*/
                   // out.close(); // 最后记得关闭文件
                }else {
                    //subFile = new File(".//result//" + index + ".txt");
                   /* if (!subFile.exists()) {
                        subFile.createNewFile();
                    }*/
                    //out = new BufferedWriter(new FileWriter(subFile, true));
                    out.write(line2 + "\r\n"); // \r\n即为换行
                    line2 = line1;
                    //out.write(line2 + "\r\n");
                    out.flush(); // 把缓存区内容压入文件
                     // 最后记得关闭文件
                }
               // line1 = br.readLine(); // 一次读入一行数据
               // line2 = br.readLine();
                if (line1 == null){
                    out.write(line1 + "\r\n");
                    //out.write(line2 + "\r\n");
                    out.close();
                }
            }

//            /* 写入Txt文件 */
//            File writename = new File(".\\result\\en\\output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件
//            writename.createNewFile(); // 创建新文件

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值