英文:
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<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 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
,然后又尝试切换到了Scanner
和nextLine()
方法。
无论哪种方法都可以,你只需要选择一种并坚持使用。
所以,如果你喜欢使用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<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);
}
Or if you prefer to go with 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论