繁体   English   中英

需要帮助将字符串写入文本文件中的多行

[英]Need help writing a string to multiple lines in a text file

我有一个试图写入文本文件的字符串数组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("");
}

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

temp = temp.substring(split, 250);

当您第一次运行代码并在temp中存储长度为250的字符串 ,此方法效果很好,因为它以temp = temp.substring(0, 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/

首先,您需要遍历包含所有字符串的数组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);
      }

   }

下一步是解决如何编写每个String的问题。 一种方法是写入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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM