Hello,
I've tried so, but misses 1st line. The command "readNextLine" reads the next (the second) line. Can you confirm?
Greetings
JIB
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello,
I've tried so, but misses 1st line. The command "readNextLine" reads the next (the second) line. Can you confirm?
Greetings
JIB
Please don't resurrect a five year old post - I have moved your post to its own thread. For the record, this post references:
http://www.javaprogrammingforums.com...ner-class.html
I recommend posting what you have tried (please wrap your code in the code tags), and explicitly define the problem you are having.
Please post the code you are having problems with. Be sure to wrap the code in code tags.
If you don't understand my answer, don't ignore it, ask a question.
Here is my code. Please have a look to the code-line after "//here is the problem ..."
private Path filteredFile() { List<LEVImportFileDto> checkDtoList = new ArrayList<LEVImportFileDto>(); LEVImportFileDto insFileDto = new LEVImportFileDto(); String filtFile = importPath + "\\Documents\\LEV\\toImport.txt"; filtFile = filtFile.replaceAll("\\\\", "/"); Path inPath; Path outPath; inPath = Paths.get(URI.create("file:/" + filetoload)); outPath = Paths.get(URI.create("file:/" + filtFile)); InputStream inStream = null; Scanner scIn = null; BufferedWriter writer = null; try { inStream = Files.newInputStream(inPath, READ); scIn = new Scanner(inStream, iso.name()); writer = Files.newBufferedWriter(outPath, utf8, WRITE, CREATE); while (scIn.hasNextLine()) { String[] line; String header; header = scIn.nextLine(); line = header.split("#"); // Überschrift fliegt raus // Ausschlusskriterien prüfen, ggf. springen if (line[12].matches("[0][123]") == false) { continue; } if (line[5].equals("000000000")){ continue; } // dto füllen insFileDto = fillFileDto(line, false); // ... prüfen int y = 0; for (LEVImportFileDto xDto : checkDtoList) { if (xDto.equals(insFileDto)) { y += 1; break; } } // ... ggf. ausschließen if (y > 0) { continue; } else { // Dto der Liste hinzufügen checkDtoList.add(insFileDto); //here is the problem: the first line to be written is the second line of the file // und Zeile schreiben writer.write(header); // writer.newLine(); } } writer.close(); // spezielle Exception des Scanners prüfen if (scIn.ioException() != null) { scIn.ioException().printStackTrace(); } } catch (Exception e) { String datumzeit = DatumUtil.formatiereDateTime(new Date()); content = Arrays.asList(datumzeit + " - ERROR: " + e.getMessage() + ", Typ " + e.getClass().getTypeName()); try { writeProtocol(content, false); } catch (IOException e1) { } e.printStackTrace(); } finally { scIn.close(); } return outPath; }
The first line of the input-file would not be written in the output-file. Why can I get the 1st line from file?
Thanks.
JIB
___
My English is like me - very bavarian
Have you tried debugging the code by adding a println() statement after every statement that reads from the file that prints out what was read? I see two continue statements that could skip the part where header is written.
If you don't understand my answer, don't ignore it, ask a question.
That are the first lines of the file:
There is no match at first line after the header with the continue-if-clauses. In debug-breakpoint-view at lineMONAT#KV_NUMMER#DIAGNOSE#ARTZNR#LANR#BSNR#TITEL#NA ME#VORNAME#STRASSE#PLZ#ORT#BERECHTIGUNG#BERECHTIGU NG_VON#BERECHTIGUNG_BIS#LIEFER_TERMIN
201408#01#01#0019538#001953830#019920600#Dr. med.#xxxx#xxxx#xxxxx#24105#Kiel#18#01.07.2008#31.1 2.2100#01.07.2014
201408#01#01#0092624#009262403#018019500#Dr. med.#xxxx#xxxx#xxxx#25336#Elmshorn#01#13.10.2008#3 1.12.2100#01.07.2014 //This line is the 1st line of output
201408#01#01#1041152#104115203#012021900#Dr. med.#xxxx#xxxx#xxxxx#23730#Neustadt#01#01.04.2012# 31.12.2100#01.07.2014
201408#01#01#3348092#334809203#012001900##xxxx#xxx x#xxxx#25764#Wesselburen#01#01.01.2012#31.12.2100# 01.07.2014
201408#01#01#3650835#365083501#018003100#Dr. med.#xxxx#xxxx#xxxx#24247#Mielkendorf#18#25.05.201 3#31.12.2100#01.07.2014
201408#01#01#4824828#482482801#018088700#Dr. med.#xxxx#xxxx#xxxx#22846#Norderstedt#01#09.04.201 3#31.12.2100#01.07.2014
201408#01#01#6087310#608731003#012065700#Dr. med.#xxxx#Christoph Wolfgang Albrecht#Bahnhofstraße 36 a#22880#Wedel#01#01.04.2006#31.12.2100#01.07.2014
I can see the second line (begins with "201408#01#01#0092624#009262403") at first. Why?header = scIn.nextLine();
Best wishes
JIB
____
My english is like me - very bavarian
Did you add the println() statements after each read of a line from the file?
What was printed?
Also use the Arrays class's toString() method to print out the contents of the line array.
If you don't understand my answer, don't ignore it, ask a question.
javainbrain (September 5th, 2014)
Oh man, what a shame!
The first line was filtered by first continue-if. Sorry, I'm blind sometimes. And thank you for the good tips.
In Germany we say: "Ich habe den Wald vor lauter Bäumen nicht gesehen!" (I haven't seen the wood because of the many trees there)
Best wishes
JIB
____
My english is like me - very bavarian
This code reads the file line by line.
public static void readFileByLine(String fileName) {
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
You can also set a delimiter as a line separator and then perform the same.
scanner.useDelimiter(System.getProperty("line.sepa rator"));
You have to check whether there is a next token available and then read the next token. You will also need to doublecheck the input given to the Scanner. i.e. dico.txt. By default, Scanner breaks its input based on whitespace. Please ensure that the input has the delimiters in right place