0

I am testing a part of a bigger program so thats the reason if it looks a bit illogical to structure it this way.The code that I am using is fairly simple but I do not understand why the first half of the lines in the file are completely missing, they not being read at all.

public class Trying {

   public static String myReader(){
        StringBuilder buf = new StringBuilder();
        BufferedReader in = null;
        String str = null;
        try {
            InputStream is = new FileInputStream("c:/themap.txt");
            in = new BufferedReader(new InputStreamReader(is));


            while ((str = in.readLine()) != null) {
                buf.append(str);
                //buf.append(System.getProperty("line.separator"));

                System.out.println(str);
            }

            in.close();

        }
        catch (IOException e) {
            e.printStackTrace();
        }

        return buf.toString();
    }


public static void main(String[] args) {

    String fmap = myReader();

    //System.out.println(fmap);

As you can see I have a basic println in the function just to see what is happening and the first lines are not being shown at all. The rest of the file is being read as it is. Maybe it has to do with the file being 5000 lines long. Any help would be appreciated.

The file's lines look like this

o AE_Cube.014
v 4.996883 0.000000 -0.663325
v 4.996883 0.336000 -0.663325
v 4.490770 0.000000 -1.013470
v 4.490770 0.336000 -1.013470
v 4.990770 0.000000 -1.070477
v 4.990770 0.336000 -1.070477
v 4.589172 0.000000 -1.269103

They are all kind of like this. They don't change in the middle to some other format

The output I get is the same but with the first 1/3 of the file missing.

f 650//652 654//652 653//652
f 649//653 651//653 654//653
f 648//654 649//654 650//654
f 651//655 647//655 648//655
f 652//656 648//656 650//656
f 650//657 649//657 654//657
f 649//658 647//658 651//658
f 648//654 647//654 649//654
o DV_Cube.059
v -1.593174 0.150000 -2.197537
v -1.617969 0.150000 -2.297979
v -1.494583 0.150000 -2.229900
v -1.525588 0.150000 -2.324470
v -1.593001 0.000000 -2.199192
v -1.616714 0.000000 -2.297979
v -1.526114 0.000000 -2.324543
v -1.495018 0.000000 -2.229900
Staykov
  • 19
  • 2

2 Answers2

0

Apparently the code works fine. My mistake is not disabling console limitations in Eclipse and it displaying incomplete string causing me to panic.

Thanks to EJP and Maarten Bodewes.

Staykov
  • 19
  • 2
-1

This should work.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Test {

public Test() {

}

public static void main(String [] args) throws IOException{

    File file = new File("myFile");
    ArrayList <String> myText = new ArrayList<String>();
    StringBuilder myTextString = new StringBuilder();

    try {
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        String line;

        while((line = br.readLine()) != null){
            myText.add(line);
            myTextString.append(line + " ");
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(myText);
    System.out.println(myTextString);
}

}

In the example i show the way for both filling an array or just building a String with stringbuilder

Albin Wärme
  • 57
  • 1
  • 5
  • 1
    "This should work" answers are generally poor answers. You need to explain a little bit why this code is answering the question and what is the problem being addressed here. No doubt that this should work for you, but what if it doesn't work for everyone? – Frederik.L Mar 20 '17 at 00:57