如何解析文本文件中的单行?

huangapple go评论63阅读模式
英文:

How to parse invidual line of a text file?

问题

public static void main(String[] args) throws IOException {
    try {
        List<String> line = Files.readAllLines(Paths.get("/home/madhu/Desktop/demo.txt"));
        Scanner scanner = new Scanner((Readable) line);
        List<FogDevice> fogdevices = new ArrayList<FogDevice>();
        while (scanner.hasNextLine()) {
            String data[] = scanner.nextLine().split(" ");
            System.out.println(data);
            fogdevices.add(createFogDevice(data[0], Boolean.parseBoolean(data[1]), Long.parseLong(data[2]),
                    Integer.parseInt(data[3]), Double.parseDouble(data[4]), Double.parseDouble(data[5]),
                    Double.parseDouble(data[6])));
        }
        scanner.close();
        System.out.println(fogdevices);
    } catch (NumberFormatException e) {
        System.out.println(e);
    }
}

private static FogDevice createFogDevice(String name2, boolean x2, long mips2, int ram2, double ratepermips2,
        double busypower2, double idlepower2) {
    // --------
    // ---------some activity-----
    // Return fogdevice ;
}

Format of demo.txt:

FD1,true,102400,4000,0.01,103,83.25
FD0,false,102400,4000,0.01,103,83.25

I want output in the below form using the createfogdevice function:

fogdevice1 : FD1,true,102400,4000,0.01,103,83.25
fogdevice2 : FD0,false,102400,4000,0.01,103,83.25
英文:
public static void main(String[] args) throws IOException{
try {
List&lt;String&gt; line = Files.readAllLines(Paths.get(&quot;/home/madhu/Desktop/demo.txt&quot;));
Scanner scanner = new Scanner((Readable) line);
List&lt;FogDevice&gt; fogdevices =new ArrayList&lt;FogDevice&gt;();
while(scanner.hasNextLine()){
String data[]=scanner.nextLine().split(&quot; &quot;);
System.out.println(data);
fogdevices.add(createFogDevice(data[0],Boolean.parseBoolean(data[1]),Long.parseLong(data[2]),Integer.parseInt(data[3]),Double.parseDouble(data[4]),Double.parseDouble(data[5]),Double.parseDouble(data[6])));
}
scanner.close();
System.out.println(fogdevices);
}catch (NumberFormatException e) {
System.out.println(e);
}
}
private static FogDevice createFogDevice(String name2, boolean x2,long mips2,int ram2, double ratepermips2,
double busypower2, double idlepower2) {
--------
---------some activity-----
return fogdevice ;
}

Format of demo.txt :

FD1,true,102400,4000,0.01,103,83.25
FD0,false,102400,4000,0.01,103,83.25

i want output in the below form by using createfogdevice function.

fogdevice1 : FD1,true,102400,4000,0.01,103,83.25
fogdevice2 : FD0,false,102400,4000,0.01,103,83.25

答案1

得分: 1

问题在于你混淆并混合了两种不同的按行读取文件的方法。你一开始使用了nio方法的readAllLines,然后又尝试切换到了ScannernextLine()方法。

无论哪种方法都可以,你只需要选择一种并坚持使用。

所以,如果你喜欢使用readAllLines

try {
    List<String> line = Files.readAllLines(Paths.get("/home/madhu/Desktop/demo.txt"));
    List<FogDevices> fogdevices = new ArrayList<>();
    for (String l : line) {
        String data[] = l.split("\\s*,\\s*");
        System.out.println(data);
        fogdevices.add(
            createFogDevice(
                data[0],
                Boolean.parseBoolean(data[1]),
                Long.parseLong(data[2]),
                Integer.parseInt(data[3]),
                Double.parseDouble(data[4]),
                Double.parseDouble(data[5]),
                Double.parseDouble(data[6])
            ));
    }
    System.out.println(fogdevices);
} catch (NumberFormatException e) {
    System.out.println(e);
}

或者如果你更喜欢使用Scanner

try {
    Scanner scanner = new Scanner(Paths.get("/home/madhu/Desktop/demo.txt"));
    List<FogDevices> fogdevices = new ArrayList<FogDevices>();
    while (scanner.hasNextLine()) {
        String data[] = scanner.nextLine().split("\\s*,\\s*");
        System.out.println(data);
        fogdevices.add(
            createFogDevice(
                data[0],
                Boolean.parseBoolean(data[1]),
                Long.parseLong(data[2]),
                Integer.parseInt(data[3]),
                Double.parseDouble(data[4]),
                Double.parseDouble(data[5]),
                Double.parseDouble(data[6])
            ));
    }
    scanner.close();
    System.out.println(fogdevices);
} catch (NumberFormatException e) {
    System.out.println(e);
}
英文:

The problem is that you're confusing and mixing two different approaches to reading a file line-by-line. You started off using the nio method readAllLines, and then tried to switch gears to a Scanner and nextLine().

Either approach will work, you just have to pick one and stick with it.

So, if you like like readAllLines:

        try {
List&lt;String&gt; line = Files.readAllLines(Paths.get(&quot;/home/madhu/Desktop/demo.txt&quot;));
List&lt;FogDevices&gt; fogdevices = new ArrayList&lt;&gt;();
for (String l : line) {
String data[] = l.split(&quot;\\s*,\\s*&quot;);
System.out.println(data);
fogdevices.add(
createFogDevice(
data[0],
Boolean.parseBoolean(data[1]),
Long.parseLong(data[2]),
Integer.parseInt(data[3]),
Double.parseDouble(data[4]),
Double.parseDouble(data[5]),
Double.parseDouble(data[6])
));
}
System.out.println(fogdevices);
} catch (NumberFormatException e) {
System.out.println(e);
}

Or if you prefer to go with Scanner:

        try {
Scanner scanner = new Scanner(Paths.get(&quot;/home/madhu/Desktop/demo.txt&quot;));
List&lt;FogDevices&gt; fogdevices = new ArrayList&lt;FogDevices&gt;();
while (scanner.hasNextLine()) {
String data[] = scanner.nextLine().split(&quot;\\s*,\\s*&quot;);
System.out.println(data);
fogdevices.add(
createFogDevice(
data[0],
Boolean.parseBoolean(data[1]),
Long.parseLong(data[2]),
Integer.parseInt(data[3]),
Double.parseDouble(data[4]),
Double.parseDouble(data[5]),
Double.parseDouble(data[6])
));
}
scanner.close();
System.out.println(fogdevices);
} catch (NumberFormatException e) {
System.out.println(e);
}

huangapple
  • 本文由 发表于 2020年10月7日 12:47:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64237379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定