Results 1 to 11 of 11
  1. #1
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default file/ array question

    How do you go about storing a file into an array?

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    More description please? Do you mean storing each word in a file? Heres a method that will put an entire .txt file into a single String:
    Java Code:
    private String readFileAsString(String filePath) throws IOException {
            StringBuilder fileData = new StringBuilder(1000);
            BufferedReader reader = new BufferedReader(
                    new FileReader(filePath));
            char[] buf = new char[1024];
            int numRead = 0;
            while((numRead=reader.read(buf)) != -1){
                fileData.append(buf, 0, numRead);
            }
            fileData.trimToSize();
            reader.close();
            return fileData.toString();
        }
    Then to put it all in an array using spaces as delimiters you could use regular expressions, i.e. String[] arr = readFileAsString("../sample.txt").split(put regex here);
    Hope this helped
    -MK12
    Last edited by MK12; 02-16-2009 at 05:11 PM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    i need to store each line into the array so i can search through the array and find a specific line that i am looking for and then add other things too the array and write it back to the file.

    I have all the code for everything except i dont know how to load the file into the array so i can edit it. Everytime it load the file it doesnt load it into the array it just loads the file and prints to the screen. When i add items to the array and write it back to the file it deletes everythign that was previously on the file.

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Just do what I showed you in my first post, except for the split method that turns the String into an Array of Strings, use a newline for the regular expression.
    For adding things to the file, just store the file as a String(use readFileAsString(String) method) then overwrite the file like this:
    Java Code:
    public void scrambleFile(String filePath) throws IOException {
            String txt = readFileAsString();
            FileWriter fw = new FileWriter(filePath);
    		BufferedWriter bw = new BufferedWriter(fw);
    		bw.write(txt + "new stuff to add to the file"));
    		bw.close();
    }
    Hope this helps.
    -MK12
    Last edited by MK12; 02-16-2009 at 05:21 PM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Store each line into an array

    To store each line from a file into an array, you can also use the scanner class (in the case below I'm putting the file's lines into an ArrayList, since I don't know how many lines the file has):
    Java Code:
    Scanner scanner = new Scanner(new File(fileName));
    while ( scanner.hasNextLine())
          {
            fileArray.add( new String(scanner.nextLine())); 
           }
    scanner.close();
    Link to the Scanner class API:
    Scanner (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    Here is my code so far for the reading the file into an array and my search method

    could someone assist me on how to fix my problem so it will read the file into the array. Thanks



    public void readFile(){

    FileInputStream read;
    DataInputStream in;
    BufferedReader br;

    GregorianCalendar calendar;
    try{
    read = new FileInputStream("H://employeefile.dat");
    in = new DataInputStream(read);
    br = new BufferedReader(new InputStreamReader(in));
    String fileRead;

    while((fileRead = br.readLine()) !=null){
    System.out.println(fileRead.toString());
    }
    in.close();
    }

    catch (IOException e){
    System.out.println("File Not Found");
    }
    }




    public String searchEmp(int find){

    int search = NOT_FOUND;

    try{
    if(isEmpty())
    throw new EmptyStackException();

    for(int i = 0; i < counter && search == NOT_FOUND; i++){
    if(employees[i].getID() == find){

    search = i;
    System.out.println(employees[search]);
    }
    System.out.println(" ");
    }
    }

    catch (EmptyStackException a){
    System.out.println("Empty");
    }


    return employees[search].toString();
    }

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Wonderful links ....

    OK... here's a link that discusses arrays:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Here's the API specification for the Array class:
    Array (Java Platform SE 6)

    Luck,
    CJS
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    anybody???

  9. #9
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    I've already told you how.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default huh?

    MayHew... Did you read the the links I posted? It's explains how to use arrays, how to populate the array elements and how to get the values from the array. You already have the loop that reads the files lines. Instead of send it each line to the screen, assign each line to an array element.

    Now... the big question... what exactly are you expecting from this forum?
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    darrenabc is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 04:44 PM
  2. Export an array to a text file
    By carderne in forum New To Java
    Replies: 32
    Last Post: 10-02-2008, 03:52 PM
  3. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 06:26 AM
  4. Data array to file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:42 AM
  5. Question abt.reading xml file using java
    By gvi in forum Advanced Java
    Replies: 6
    Last Post: 11-08-2007, 05:48 PM

Posting Permissions

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