java写入txt文件 想要修改txt文件每一行的第一个数字 加一就好

txt文件是
1 1 5
2 2 10
3 3 15

4 4 20
请问怎么写代码
到时会有很多行的 每行的第一个数字都要加一

  java实现向txt每行增加一位数字,思路是这样的:使用I/O操作每次读取一行文字,使用string增加一个数字一,保存在缓存另一个list里面,后接一个换行符,等到全部读取完毕,在读取list的内容,写入txt文件里面,示例如下:

package com.zeal.card; // è¿™é‡Œæ˜¯æˆ‘自己临时用的包名,你自己改一下就好了

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Demo {

    /**
     * ä¸»æ–¹æ³•
     * @param args
     */
    public static void main(String[] args) {
        printData();
    }

    /**
     * è¯»å–txt文档第一行数据中的第3位到第9位,并输出到控制台
     */
    public static void printData() {

        // å®šä¹‰æ–‡æœ¬æ–‡ä»¶æ•°ç»„,这里是临时演示用,请自己改写
        String[] txtFiles = {
                "c:/a.txt",
                "c:/b.txt",
                "c:/c.txt",
        };

        // éåŽ†æ–‡ä»¶
        for (int i=0; i<txtFiles.length; i++) {
            try {
                // å¾—到文件
                File file = new File(txtFiles[i]);
                // å¦‚果文件存在
                if (file.exists()) {
                    // å»ºç«‹ç¼“冲包装器
                    BufferedReader in = null;
                    in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                    // è¯»å‡ºä¸€è¡Œï¼ˆå› ä¸ºåªæ˜¯è¯»ä¸€è¡Œï¼Œæ²¡å¿…要遍历全部文件内容)
                    String temp = in.readLine();
                    // å¦‚果不为空,并且长度至少为9
                    if (temp != null) {
                       
                            String txt = "一"+temp;//每行前面增加一个数字一。
                            System.out.println("取出数据:" + txt);
                            List li= new ArrayList();
                            List li= new ArrayList();
            li.add(temp);
                        }
                    }
                    BufferedWriter in = null;
                    in = new BufferedWriter (new InputStreamWriter(new FileOutputStream(file)));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-03-02

写了一个简易的方案,但是效率比较低,如果有更好的方案还望不吝赐教;(不知道正则是否更方便些?)

按行读取文件,然后按照空格分组,对第一个数字加1,然后写入新的文件。


public static void writeFile() {
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try{
        File file = new File("new.txt");
        if(!file.exists()) {
            file.createNewFile();
    }
    StringBuffer sb = new StringBuffer();
    reader = new BufferedReader(new FileReader("test.txt"));
    String line = null;
    //按行读取
    while((line = reader.readLine()) != null) {
        String[] arr = line.split("[ \t]++");
        if(arr.length < 3) {
        sb.append(line).append("\r\n");
        continue;
    }
    //获取第一个数字,并加1
    int num = Integer.valueOf(arr[0]);
    num ++;
    sb.append(num).append("\t").append(arr[1]).append("\t").append(arr[2]).append("\r\n");
    }
    //写入新的文件
    writer = new BufferedWriter(new FileWriter(file));
    writer.write(sb.toString());
    }catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
}

追问

怎么我一运行 就提示找不到符号

本回答被提问者采纳
第2个回答  2014-03-02
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

先读 ,然后修改,再写入txt。 笨方法,如果数据量大,不建议使用,只获得第一个字符,就好。
第3个回答  2014-03-02

写了一个简易的方案,但是效率比较低,如果有更好的方案还望不吝赐教;(不知道正则是否更方便些?)

按行读取文件,然后按照空格分组,对第一个数字加1,然后写入新的文件。


public static void writeFile() {
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try{
        File file = new File("new.txt");
        if(!file.exists()) {
            file.createNewFile();
    }
    StringBuffer sb = new StringBuffer();
    reader = new BufferedReader(new FileReader("test.txt"));
    String line = null;
    //按行读取
    while((line = reader.readLine()) != null) {
        String[] arr = line.split("[ \t]++");
        if(arr.length < 3) {
        sb.append(line).append("\r\n");
        continue;
    }
    //获取第一个数字,并加1
    int num = Integer.valueOf(arr[0]);
    num ++;
    sb.append(num).append("\t").append(arr[1]).append("\t").append(arr[2]).append("\r\n");
    }
    //写入新的文件
    writer = new BufferedWriter(new FileWriter(file));
    writer.write(sb.toString());
    }catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
}

追问

怎么我一运行 就提示找不到符号

本回答被提问者采纳
第4个回答  2014-03-02
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

先读 ,然后修改,再写入txt。 笨方法,如果数据量大,不建议使用,只获得第一个字符,就好。
相似回答
大家正在搜