package inputOutput;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Test_leser {
public static void main(String[] args) throws IOException {
// asking which file to open (no abändre das es dynamisch übere Browser funktioniert)
Scanner scan = new Scanner(System.in);
System.out.println("Enter Filename ");
BufferedReader bReader = null;
// Open demanded file
try {
bReader = new BufferedReader(
new FileReader("C:\\Users\\valat\\OneDrive\\Desktop\\image-data\\" + scan.next()));
} catch (FileNotFoundException fnfex) {
System.out.println(fnfex.getMessage());
// Programm geht nicht weiter, falls file not found
System.exit(0);
}
String line;
String resolution = null;
String image = null;
String description = null;
int i = 0;
// assigning values to variables
while ((line = bReader.readLine()) != null) {
// zähler
i += 1;
if (line.startsWith("description:")) {
description = line;
}
else if (line.startsWith("image-file")) {
image = line;
}
else if (line.startsWith("resolution:")) {
resolution = line;
} else {
System.out.println("something is REALLY wrong");
}
System.out.println(description);
System.out.println(image);
System.out.println(resolution);
}
}
}
Hey. I'm having trouble here. I'm reading this text with a BufferedReader from a textfile:
description: Blutausstrich (Mensch)
image-file: image01.jpg
resolution: 0.002 mm
after that my next intention is to iterate through each line, and assign each line on its own to a variable. The problem is, my output, whenever I try it, is the following:
description: Blutausstrich (Mensch)
null
null
description: Blutausstrich (Mensch)
image-file: image01.jpg
null
description: Blutausstrich (Mensch)
image-file: image01.jpg
resolution: 0.002 mm
As you can probably see, that isn't exactly what I want. How can I assign just the specific lines which fullfill my if-condition from the txtfile with my variables? I would really appreciate some help, I'm trying to play aroung with this one for hours, I looked up so many different things, and I'm pretty sure it's a pretty obvious answer which will make me think "oh bloody hell, seriously? I'm a moron".
Thank you guys in advance!
Aucun commentaire:
Enregistrer un commentaire