Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: extracting file content into array

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:

    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: extracting file content into array

    But the problem is that it returns an IOException and
    Please post error messages with your code and question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: extracting file content into array

    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)

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: extracting file content into array

    Found the solution. Had the data folder in the wrong place. Thanks for your help!

Similar Threads

  1. extracting a list of few lines from html file
    By Vaibhav.forpro in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: April 14th, 2012, 08:35 AM
  2. [SOLVED] Access Denied when extracting zip file.
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 18th, 2011, 07:50 PM
  3. Problem extracting data from file
    By hello_world in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: August 21st, 2011, 09:35 PM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Extracting the BINDING element from WSDL file
    By Sai in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2010, 02:56 AM