【问题标题】:Start with new line after certain amount of characters in java在java中一定数量的字符之后从新行开始
【发布时间】:2014-02-10 09:10:57
【问题描述】:

我有一个读取文件的程序,我可以更改该文件的内容,然后将其写入另一个文件。输入文件看起来像这样:http://gyazo.com/4ee1ade01378238e2c765e593712de7f 输出必须看起来像这样 http://gyazo.com/5a5bfd00123df9d7791a74b4e77f6c10 我当前的输出是 http://gyazo.com/87a83f4c6d48aebda3d11060ebad66c2 那么如何更改我的代码,它在 12 个字符后开始新行?我也想删除最后一个!。

    public class readFile {

        String line;
        StringBuilder buf = new StringBuilder();


        public void readFile(){

        BufferedReader reader = null;

        try {
        File file = new File("C:/Users/Sybren/Desktop/Invoertestbestand1.txt");
        reader = new BufferedReader(new FileReader(file));

        //String line;
        while ((line = reader.readLine()) != null) {
        System.out.println(line);
        //buf.append(line);
        processInput();
        }

        } catch (IOException e) {
        e.printStackTrace();
        } finally {
        try {
        reader.close();
        } catch (IOException e) {
        e.printStackTrace();
        };
        }
        }
        public void processInput(){

        buf.append(line);  

        if (buf.length()>7){
        buf.append("-");
        //buf.append(System.getProperty("line.separator"));
        }

        /* start with a new line if the line length is bigger than 12 - in progress*/
        /* I know this if doesn't work but how to fix it? */
        if (buf.length()>12){
        buf.append(System.getProperty("line.separator"));
        }

        /* if a * is followed by * change them to a !*/
        for (int index = 0; index < buf.length(); index++) {
        if (buf.charAt(index) == '*' && buf.charAt(index+1) == '*') {
        buf.setCharAt(index, '!');
        buf.deleteCharAt(index+1);
        //buf.deleteCharAt(buf.length()-1);
        }
        // get last character from stringbuilder and delete
        //buf.deleteCharAt(buf.length()-1);


        } 

        }

        public void writeFile() {
        try {

        String content = buf.toString();

        File file = new File("C:/Users/Sybren/Desktop/test.txt");

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.close();

        System.out.println("Done");


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

}

【问题讨论】:

  • 最后是什么? 12 的最后一个字符?
  • 当前输出中没有最后一个感叹号
  • 您基本上是想将文件作为字符串获取,删除换行符,然后在每 12 个字符后输出一个换行符吗?
  • 我想读取文件,进行一些更改并将其写入另一个文件。其中一项更改是每 12 个字符后开始换行。

标签: java bufferedreader stringbuilder bufferedwriter


【解决方案1】:

更新在读取文件时您将做出决定的代码:

        int sevenCount = 0;
        int fourteenCount = 0;
        int data = 0;
        while ((data = reader.read()) != -1) {
            sevenCount++;
            fourteenCount++;
            if(sevenCount==7)  
            {
                 buf.append("-");   // append - at every 7th character
                 sevenCount = 0;
            }
            if(fourteenCount==14)
             {
                 buf.append("\n");  // change line after evrry 14th character
                 fourteenCount = 0;
              }

            if(((char)data) == '*')
            {
                  char c = '!';    //Change the code when char contain *
                  data = (int)c;
             }
            else
            {
                  buf.append((char)data);   
            }
        }

【讨论】:

  • 我认为你的意思是 "\n" 而不是 "\\n" 和 buf.append(System.getProperty("line.separator"));也可以,但我想知道如何在 12 个字符后开始新行,这就是问题
  • @Sybren 实际上由于防火墙,我无法访问附件,所以我错过了 pblm。请给我一些时间给你正确的答案。
  • @Sybren 可以粘贴输入和预期输出,而不是附加文件。
  • 当我尝试在此处或在我的问题中复制/粘贴输入/输出时,某些字符会自动更改(不要问我为什么)。所以请看看提供的链接。
  • 不幸的是它不起作用,只有第一行的长度正确(我的代码也是如此)并且你用 !但是后面的两个 * 必须用 ! 代替。另一件事是我必须使用 .readLine() 而不是 .read() 进行阅读。
【解决方案2】:

如果你想在字符串中每 12 个字符插入一个换行符:

str = str.replaceAll(".{12}", "$0\n");

【讨论】:

  • 不错的方法,但缺点是需要将整个文件加载到字符串中才能正常工作。这会给大文件带来问题。
  • 除非您的文件很大,否则我会使用 StringBuffer 来构建新内容,然后通过上述调用传递其toString(),然后将其写入新文件。
猜你喜欢
  • 2021-01-19
  • 2012-12-28
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多