Java 读取带分隔符的文件并按顺序输出

Java 读取带分隔符的文件并按顺序输出,java,Java,我有下面的文件fruit.txt,我想在文件中读取并按顺序输出它们 我所做的是,我尝试使用嵌套for循环来获取列表,但失败了。 我不确定如何在循环中添加更多记录 BufferedReader brInput = new BufferedReader(new FileReader("fruit.txt")); String line; String x = null; while ((line = brInput.readLine()) != null) { String[] fruit =

我有下面的文件fruit.txt,我想在文件中读取并按顺序输出它们

我所做的是,我尝试使用嵌套for循环来获取列表,但失败了。 我不确定如何在循环中添加更多记录

BufferedReader brInput = new BufferedReader(new FileReader("fruit.txt"));
String line;
String x = null;
while ((line = brInput.readLine()) != null) {
   String[] fruit = line.split("\\.");

    for(int i =0; i<fruit.length;i++){
        for(int j =0; j<fruit.length;j++){
             x = fruit[i]+"."+fruit[j];
            System.out.println(x);
        }
    }
}
fruit.txt:

apple.orange.pear.grape
apple.orange.pear.grape.melon
apple.orange.pear.grape.melon.Apricot
banana.berry.Avocado
预期产出:

apple
apple.orange
apple.orange.pear
apple.orange.pear.grape

apple
apple.orange
apple.orange.pear
apple.orange.pear.grape
apple.orange.pear.grape.melon

apple
apple.orange
apple.orange.pear
apple.orange.pear.grape
apple.orange.pear.grape.melon
apple.orange.pear.grape.melon.Apricot

banana
banana.berry
banana.berry.Avocado

您当前正在做的是将每个数组元素与每个数组元素(甚至是相同的数组元素)组合起来。似乎您希望在每次迭代中添加一个数组元素,因此您应该这样做:

StringBuilder sb = new StringBuilder();
for( String word : fruit) {
  //if there's already something in the StringBuilder add a dot before adding the word
  if( sb.length() > 0 ) {
    sb.append( "." );
  }

  sb.append( word );
  System.out.println( sb.toString() );
}
假设水果数组是
{“apple”、“orange”、“pear”、“grape”}
这将输出

apple
apple.orange
apple.orange.pear    
apple.orange.pear.grape

您只需遍历字符串数组一次。将第一个值指定给
字符串x
,然后将数组中的每个后续项追加到
x
。您可以修改for循环,如下所示:

for(int i = 0; i < fruit.length; i++) {
  if(i == 0) {
    x = fruit[i];
  } else {
    x += "." + fruit[i];
  }
  System.out.println(x);
}
for(int i=0;i
将以下代码与扫描仪库一起使用,这将完美工作到结束:

File f=new File("C:/Users/User1/Desktop/t.txt");

            Scanner s=new Scanner(f);
            s.useDelimiter("\r\n");
            String d=null;
            String temp=null;
            String da[]=null;

            int i=0;

            d=s.nextLine();

            while(s.hasNextLine()){                 
                d=d.replace(".", ",");
                da=null;
                da=d.split(",");

                temp=null;
                i=0;

                while(i<da.length){
                    if(i==0){
                        temp=da[i];
                    }
                    else{
                temp=temp+"."+da[i];    

                    }

JOptionPane.showMessageDialog(null,temp);  //-----------Output-------------
                i++;
                }                   
                d=null;
                d=s.nextLine();
                }   
            s.close();
File f=新文件(“C:/Users/User1/Desktop/t.txt”);
扫描器s=新扫描器(f);
s、 使用分隔符(“\r\n”);
字符串d=null;
字符串temp=null;
字符串da[]=null;
int i=0;
d=s.nextLine();
而(s.hasNextLine()){
d=d。替换(“.”,“,”);
da=null;
da=d.拆分(“,”);
温度=零;
i=0;

虽然(这是公认的答案!?使用Thomas建议的方法,更好地构建这样的字符串
File f=new File("C:/Users/User1/Desktop/t.txt");

            Scanner s=new Scanner(f);
            s.useDelimiter("\r\n");
            String d=null;
            String temp=null;
            String da[]=null;

            int i=0;

            d=s.nextLine();

            while(s.hasNextLine()){                 
                d=d.replace(".", ",");
                da=null;
                da=d.split(",");

                temp=null;
                i=0;

                while(i<da.length){
                    if(i==0){
                        temp=da[i];
                    }
                    else{
                temp=temp+"."+da[i];    

                    }

JOptionPane.showMessageDialog(null,temp);  //-----------Output-------------
                i++;
                }                   
                d=null;
                d=s.nextLine();
                }   
            s.close();