Results 1 to 8 of 8
- 11-06-2012, 04:40 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
extracting file content into array
Hello, I am fairly new to Java and I'm working on some code that is supposed to open a file and copy each line into an array. But the problem is that it returns an IOException and doesn't complete the task. The code is below:
Any help will be appreciated.Java Code:import java.util.*; import java.io.*; public class test { static String[] subjects = {}; public static void main(String[] args) { loadArrays(); } public static void loadArrays() { String subFile = "data/subjects.txt"; try { subjects = readLines(subFile); } catch(IOException e) { System.out.println("cant"); } } public static String[] readLines(String filename) throws IOException { FileReader fileReader = new FileReader(filename); BufferedReader bufferedReader = new BufferedReader(fileReader); List<String> lines = new ArrayList<String>(); String line = null; while((line = bufferedReader.readLine()) != null) { lines.add(line); } bufferedReader.close(); return lines.toArray(new String[lines.size()]); } }
- 11-06-2012, 05:05 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Re: extracting file content into array
You need to supply us with the full exception (plus stack trace) and highlight the line on which it occurs.
Please do not ask for code as refusal often offends.
- 11-06-2012, 05:13 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: extracting file content into array
getStackTrace() outputs: [Ljava.lang.StackTraceElement;@56e5b723
getMessage() outputs: data/subjects.txt (No such file or directory)
- 11-06-2012, 05:30 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Re: extracting file content into array
e.printStackTrace();
Please do not ask for code as refusal often offends.
- 11-06-2012, 05:33 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: extracting file content into array
printStackTrace() outputs:
java.io.FileNotFoundException: data/subjects.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:120)
at java.io.FileInputStream.<init>(FileInputStream.jav a:79)
at java.io.FileReader.<init>(FileReader.java:41)
at test.readLines(test.java:28)
at test.loadArrays(test.java:18)
at test.main(test.java:10)
- 11-06-2012, 06:09 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Re: extracting file content into array
There you go.
It can't find the file at that location.
Try:
You'll need to check the spelling etc of the method name, and import the File class.Java Code:File f = new File("data/subjects.txt"); System.out.println(f.getAbsolutePath());
That will tell you where your code is looking for the file.Please do not ask for code as refusal often offends.
- 11-06-2012, 08:46 PM #7
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: extracting file content into array
It's looking in the correct place but it still returns that error.
- 11-06-2012, 08:53 PM #8
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Extracting values from textfile with semi-random content
By Kenjitsuka in forum New To JavaReplies: 9Last Post: 03-02-2011, 11:52 PM -
Extracting a Row/Column from An Array
By besweeet in forum New To JavaReplies: 10Last Post: 04-18-2010, 07:56 AM -
Extracting JAR file
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks