简体   繁体   中英

Reading txt file from working directory in Java (not working)

I have read a couple of posts of how to read a txt file into an int[] and through the multiple ways possible I haven't succeeded in any.

import java.io.*;
import java.util.Scanner;

public class SequenceSquare
{
private int[] arr;

public SequenceSquare() 
{
    int count = 0;
    File fileName = new File("Sequence.txt"); // fileName for opening
    try
    {
        Scanner input = new Scanner(fileName);
        while (input.hasNextLine()) //while there is a line to read
        {
            if(input.hasNextInt()) //if this line has an int
            {
                count++; // increment count 
                input.nextInt(); //move to next integer
            }
        }

        arr = new int[count]; // create arr with sufficient size
        input.close(); // close scanning file

        Scanner newInput = new Scanner(fileName);

        for(int i = 0; i < arr.length; i++)
        {
            while (newInput.hasNextLine()) // same as above
            {
                if(newInput.hasNextInt()) // same as above
                {
                   arr[i] = newInput.nextInt(); //store int scanned into arr 
                }
            }
        }
        newInput.close();
    }
    catch(FileNotFoundException f)
    {
        f.printStackTrace();
    }
}
"Sequence.txt"
1
2
5
9
29
30
7
9
111
59
106
130
-2

so basically when the default constructor is called it is suppose to open the "Sequence.txt" and read the integers that are formatted into an int array.In the txt file the numbers are formatted to be an integer per line such as 4\\n 5\\n etc. However when I iterate through the array "arr" it seems to have no contents. I have implemented a number of test functions to test whether has been filled(not listed) but the arr does not return anything. Please help. Please tell me what is happening. I'd rather know the explanation to what is happening rather than the answer but both will do. Also I know you can use arraylists and lists to carry out the same functions but I want it to be an array. If you want to see the whole class I can post but the other code is unnecessary as it works

This is how we can do it using array. If it was list it would simpler.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileReading {
    public static final String FILE_PATH = "D:\\testing.txt";
    public static void main(String[] args) {        

        try {
            int[] newArr = readFile(FILE_PATH);
            //testing the new array
            for(int i=0;i<newArr.length;i++){
                System.out.println(newArr[i]);
            }

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

    }

    public static int[] readFile(String pathToFile) throws NumberFormatException, IOException{
        String sCurrentLine;
        int[] arr = new int[countLines(pathToFile)];
        BufferedReader br = new BufferedReader(new FileReader(pathToFile));
        int i=0;
        while ((sCurrentLine = br.readLine()) != null) {                
            arr[i] = Integer.parseInt(sCurrentLine);
            i++;
        }
        br.close();
        return arr;
    }

    public static int countLines(String pathToFile) throws NumberFormatException, IOException{
        BufferedReader br = new BufferedReader(new FileReader(pathToFile));
        int count =0;
        while ((br.readLine()) != null) {
            count++;
        }
        br.close();
        return count;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM