2014-12-06 42 views
1

我遇到的问题是将简单文本文件的每一行插入到我想要的特定变量中。例如我的文本文件是有序像这样(在字符串之间的文本文件中没有空间只有这个编辑器就会把它们放在一起)如何将文本读取字符串插入到特定变量

达拉斯

78˚F

北,15英里每小时

dallasimage

丹佛

29女性

南,10英里每小时

denverimage

,我想达拉斯市的变量,78F的温度变化,并依此类推,直到文本结束,此刻是所有进入城市变量,它所有打印从那里!我知道扫描仪有一个下一行,但从来没有使用过,所以我被困在这里。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 

public class Textreader { 

    public static void main(String[] args) { 
     try { 
         String city ="a"; 
         String temp ="b"; 
         String wind ="c"; 
         String image = "d"; 
      File file = new File("load.txt"); 
      FileReader fileReader = new FileReader(file); 
      BufferedReader bufferedReader = new BufferedReader(fileReader); 
      StringBuffer stringBuffer = new StringBuffer(); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
           city = stringBuffer.append(line).toString(); 
           stringBuffer.append("\n"); 
           //city = line.toString(); 
           temp = stringBuffer.append(line).toString(); 
           wind = line; 
           image = line; 
       //stringBuffer.append(line); 
       //stringBuffer.append("\n"); 
      } 
      fileReader.close(); 
      System.out.println("Contents of file:"); 
         System.out.println(city); 
         System.out.println(temp); 

      //System.out.println(stringBuffer.toString()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

您的问题与事实,你的变量“行”不改变他的内容,并重复所有变量。将所有行读入集合或数组,然后进行处理。类似这样的:

 

    int count = 0; 
    String[] lines = new String[4]; 
    String line; 
    while ((line = bufferedReader.readLine()) != null) { 
     lines[counter++] = line; 
    } 

    city = lines[0]; 
    temp = lines[1]; 
    // .... 
+0

我这样做,而是它确实增量,但它保持的一切读取前一行并包括它。例如,对于[1]行,它读达拉斯,78 f,行[2]它读达拉斯,78F北,15英里 – user3034262 2014-12-06 06:29:57

+0

嗨,我的坏。这件事太快了。我更正了代码。这是一个简单的例子,并不是解决问题的最佳方法。不过,它会为你做这项工作。希望这可以帮助。 – Apokai 2014-12-06 23:27:06

0

您必须致电bufferedReader.readLine()来检索下一行。

while ((line = bufferedReader.readLine()) != null) { 
    city = line; 
    temp = bufferedReader.readLine(); 
    wind = bufferedReader.readLine(); 
    image = bufferedReader.readLine(); 
} 
+0

当我尝试它时,它返回null。 – user3034262 2014-12-06 06:28:42

+0

我是否必须将它们转换为字符串,我也试过,但所有返回null。 – user3034262 2014-12-06 07:34:54

0

查看java.util.Scanner类。你可以用它做这样的事情:

Scanner s = new Scanner(file); 
Pattern pattern = Pattern.compile("(.+?)\n(.+?)\n(.+?)\n(.+?)\n"); 
while(s.findWithinHorizon(pattern, 0) != null) { 
    MatchResult result = s.match(); 
    city = result.group(1); 
    temp = result.group(2); 
    wind = result.group(3); 
    img = result.group(4); 
    // etc. (Note that the next pass through the loop will override these variables 
    // so, unless you save the values somewhere, you will end up with them holding last 
    // entry from your file once the loop is done. 
} 
+0

我不得不在程序中进一步使用缓冲读取器,所有上述建议都无济于事。 – user3034262 2014-12-06 07:16:19

+0

那么,你不能在“稍后在程序中”使用同一个阅读器,因为无论如何你都已经阅读了所有的东西。如果“没有”帮助,可能意味着你误解或误用了它。如果你想得到更多的帮助,就必须提供比“没有帮助”更多的信息.​​.....并展示你的工作。 – Dima 2014-12-06 18:46:32

相关问题