Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要帮助将字符串写入文本文件中的多行吗_Java_String_Text_While Loop_Bufferedwriter - Fatal编程技术网

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

Java 需要帮助将字符串写入文本文件中的多行吗,java,string,text,while-loop,bufferedwriter,Java,String,Text,While Loop,Bufferedwriter,我有一个字符串数组toFile[],我正试图将其写入文本文件。在测试时,我被提醒,如果sting到达窗口的边界,记事本不会将字符串包装到下一行。作为一个完美主义者,我想将字符串拆分为250个字符长的子字符串,并将每个子字符串写入文件,在每个子字符串后插入新行 在测试过程中,我遇到的问题似乎无法解决,就是我的程序将在循环中运行一次,然后由于错误而失败 输出和错误示例: toFile[2].length = 2432 temp.length = 250 split = 250 iLength =

我有一个字符串数组
toFile[]
,我正试图将其写入文本文件。在测试时,我被提醒,如果sting到达窗口的边界,记事本不会将字符串包装到下一行。作为一个完美主义者,我想将字符串拆分为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[]
,并在另一个函数中逐个处理它们的写入

   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();
      }

   }
private static void write(字符串行,BufferedWriter bw)引发IOException{
int length=line.length();
如果(长度>拆分){
整数偏移=0;
while(偏移量<长度){
剩余整数=长度-偏移量;
写入(行、偏移量、剩余<拆分?剩余:拆分);
换行符();
偏移量+=分割;
}         
}否则{
写入(行);
换行符();
}
}
就这些。但是,如果字符串包含文本,分割成250个字符的块可能不是最好的选择,因为你可以切掉单词


完整示例代码:

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

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

第二次
拆分为250
,方法执行为
temp=temp.substring(250250)和温度长度变为
0

但下一次开始索引超出结束索引时,即
temp=temp.substring(500250)在您的案例中引发错误

因此,每次使用子字符串或可以执行的操作时,都要增加结束索引。。
temp=温度子串(拆分,拆分+250)


对于Java上的其他有趣和解决问题的帖子,您可以访问

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