Results 1 to 8 of 8
  1. #1
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default 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:


    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()]);
    	}
    
    }
    Any help will be appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

  3. #3
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: extracting file content into array

    getStackTrace() outputs: [Ljava.lang.StackTraceElement;@56e5b723

    getMessage() outputs: data/subjects.txt (No such file or directory)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: extracting file content into array

    e.printStackTrace();
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default 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)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: extracting file content into array

    There you go.
    It can't find the file at that location.
    Try:
    Java Code:
    File f = new File("data/subjects.txt");
    System.out.println(f.getAbsolutePath());
    You'll need to check the spelling etc of the method name, and import the File class.

    That will tell you where your code is looking for the file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: extracting file content into array

    It's looking in the correct place but it still returns that error.

  8. #8
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: extracting file content into array

    ok I figured out the solution. I had the data folder in the wrong place. Thank you for your guidance. It led me to the answer.

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  3. Replies: 9
    Last Post: 03-03-2011, 12:52 AM
  4. Extracting a Row/Column from An Array
    By besweeet in forum New To Java
    Replies: 10
    Last Post: 04-18-2010, 08:56 AM
  5. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •