【问题标题】:Need help writing a string to multiple lines in a text file需要帮助将字符串写入文本文件中的多行
【发布时间】:2014-09-11 01:35:10
【问题描述】:

我有一个字符串数组toFile[],我正试图将其写入文本文件。在测试时,我被提醒如果刺痛到达窗口的边界,记事本不会将字符串换行到下一行。作为一个完美主义者,我想将字符串拆分为长度为 250 个字符的子字符串,然后将每个子字符串写入文件,在每个子字符串之后插入一个新行。

在测试时,我遇到并且似乎无法解决的问题是,我的程序将通过循环运行一次,然后失败并出现错误。

输出和错误示例:

toFile[2].length = 2432

temp.length = 250
split = 250
iLength = 2182

temp.length = 0
split = 500
iLength = 1932

java.lang.StringIndexOutOfBoundsException: String index out of range: -250

我的代码:

System.out.println("toFile[2].length = "+Integer.toString(toFile[2].length()));
System.out.println("");
if(toFile[2].length()>250){
    int iLength=toFile[2].length(), split = 0;
    while(iLength>250){
        String temp = toFile[2];
        temp = temp.substring(split, 250);
        System.out.println("temp.length = "+Integer.toString(temp.length()));
        bw.write(temp);bw.newLine();
        split=split+250;
        System.out.println("split = "+Integer.toString(split));
        iLength=iLength-250;
        System.out.println("iLength = "+Integer.toString(iLength));
        System.out.println("");
    }
    bw.write(toFile[2].substring(split));
}else{bw.write(toFile[2]);bw.newLine();}
bw.newLine();

我也试过这个 while 循环,它贯穿整个字符串,但仍然只将字符串写入一行:

int iLength=toFile[2].length(), start = 0;
String temp = toFile[2];
while(iLength>250){
    bw.write(temp,start,250);

    start=start+250;
    System.out.println("start = "+Integer.toString(start));

    iLength=iLength-250;
    System.out.println("iLength = "+Integer.toString(iLength));
    System.out.println("");
}

【问题讨论】:

  • 你需要一个嵌套循环(所以总共两个循环)。外部循环遍历数组 toFile 中的所有字符串。内部循环将字符计数为 250。
  • @markspace 该数组仅包含 9 个字符串。在这 9 个中,我只需要检查一些长度即可。
  • 你仍然需要两个循环。尝试创建一个除了将字符串拆分为请求的长度之外什么都不做的方法。它将帮助您将问题分解为较小的问题。然后为您需要检查的字符串调用该方法。

标签: java string text while-loop bufferedwriter


【解决方案1】:

只需更正您代码中的一件事,我希望您的其余代码能够正常工作并且不会给出当前错误。进行以下修正。 在下面的语句中,您正在修复结束索引的值,即 250。

temp = temp.substring(split, 250);

当您第一次运行代码并在 temp 中存储长度为 250 的字符串时,这工作正常,因为它以 temp = temp.substring(0, 250); 执行,因为 split=0

第二次 split become 250,方法以temp = temp.substring(250, 250); 执行,temp.length 变为0

但是下次开始索引超出结束索引时,即temp = temp.substring(500, 250);,这会在您的情况下引发错误。

因此,每次获取子字符串时都增加结束索引,或者您可以这样做.. temp = temp.substring(split, split + 250);

有关 Java 的其他有趣和解决问题的帖子,您可以访问http://www.codingeek.com/

【讨论】:

    【解决方案2】:

    首先,您需要遍历包含所有字符串的数组toFile[],并在另一个函数中逐个处理它们的写入。

       public static void main (String[] args) throws java.lang.Exception
       {
          String[] toFile = new String[]{ ... };
          BufferedWriter bw = new BufferedWriter(...);
    
          for (String line : toFile) {   
             write(line, bw);
          }
    
       }
    

    下一步是解决如何编写每个字符串。一种方法是编写 250 个字符的块。唯一需要检查的是最后一部分可以小于250,以免StringIndexOutOfBoundsException

       private static void write(String line, BufferedWriter bw) throws IOException {
          int length = line.length();
    
          if (length > SPLIT) {         
             int offset = 0;
             while (offset < length) {
                int remaining = length - offset;            
                bw.write(line, offset, remaining < SPLIT ? remaining : SPLIT);
                bw.newLine();            
                offset += SPLIT;
             }         
          } else {         
             bw.write(line);         
             bw.newLine();
          }
    
       }
    

    仅此而已。但是如果字符串中包含文本,那么分成 250 个字符的块可能不是最好的选择,因为您可以截断单词。

    完整示例代码:http://ideone.com/jQKe7Y

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多