0

I have a question about the snippet of code which I have underneath. I have a file called FILNAVN which is a file where the info the program needs is located. The first line of said file is a row of integers which I will only need read once. Will the code acutally do this? I ask because I haven't programmed the code inside the while-loop yet, and I could actually needs some tips as to how to do this as well :P

try{
    Scanner leseFraFila= new Scanner(new File(FILNAVN)).useDelimiter(";");
    int maaned=leseFraFila.nextInt();
    int aar=leseFraFila.nextInt();
    int totalFortjeneste=leseFraFila.nextInt();
    int totaltAntallMaaneder=leseFraFila.nextInt();
    int maanedsleieVanligHybel=leseFraFila.nextInt();
    int maanedsleieToppHybel=leseFraFila.nextInt();

    while(FILNAVN.hasNext()){
         //Here is where I will probably use:
         //String linje=leseFraFila.nextLine()
         //linje.split(";");
         //String .... = 
         //int ....=
    }
}catch(IOException e){
    System.out.print(e);
}
Makri
  • 331
  • 2
  • 4
  • 15

1 Answers1

0

Here is a short, and simply solution to your problem:

static String readFile(String path, Charset encoding) throws IOException
{
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}

All you have to do now is get the information to a string:

String readContent = readFile("test.txt", StandardCharsets.UTF_8);

OR you can use the defaultCharset().

All credit goes to erickson at his very informative post here

Community
  • 1
  • 1