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.

Results 1 to 9 of 9

Thread: Re: Reading a file line by line using the Scanner class

  1. #1
    Junior Member javainbrain's Avatar
    Join Date
    Sep 2014
    Location
    Munich/Bavaria
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Re: Reading a file line by line using the Scanner class

    Hello,

    I've tried so, but misses 1st line. The command "readNextLine" reads the next (the second) line. Can you confirm?

    Greetings
    JIB


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reading a file line by line using the Scanner class

    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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading a file line by line using the Scanner class

    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.

  4. #4
    Junior Member javainbrain's Avatar
    Join Date
    Sep 2014
    Location
    Munich/Bavaria
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file line by line using the Scanner class

    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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading a file line by line using the Scanner class

    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.

  6. #6
    Junior Member javainbrain's Avatar
    Join Date
    Sep 2014
    Location
    Munich/Bavaria
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file line by line using the Scanner class

    That are the first lines of the file:
    MONAT#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
    There is no match at first line after the header with the continue-if-clauses. In debug-breakpoint-view at line
    header = scIn.nextLine();
    I can see the second line (begins with "201408#01#01#0092624#009262403") at first. Why?
    Best wishes
    JIB
    ____
    My english is like me - very bavarian

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading a file line by line using the Scanner class

    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.

  8. The Following User Says Thank You to Norm For This Useful Post:

    javainbrain (September 5th, 2014)

  9. #8
    Junior Member javainbrain's Avatar
    Join Date
    Sep 2014
    Location
    Munich/Bavaria
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file line by line using the Scanner class

    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

  10. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Reading a file line by line using the Scanner class

    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

Similar Threads

  1. Re: Reading a file line by line using the Scanner class
    By rajaveluswamy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2013, 03:38 AM
  2. [SOLVED] reading a file line by line
    By shenaz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 20th, 2013, 11:10 AM
  3. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM