Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default Export an array to a text file

    I've got a rudimentary music organising program of sorts, that imports iTunes playlists and lets you make playlists and organize things etc.

    I have no Java training, and learn only by fiddling and trial and error, and my minimal ability has not allowed me to figure out how to export my String arrays to text files.

    I have a String array like this: String[400][4]
    This allows for 400 songs, with 4 bits of information about each.

    How can I export this to a text file? (I also need it bo tab-delimited and to have a browse window to save the file, but I'm sure I can figure that out myself)

    I just need the method:
    Java Code:
    boolean exportPlaylist(int playlistNum) {
          boolean success;
          //code to add delimiting tabs
          //code to export to a playlist
         return success;
    }//exportPlaylist

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to tie the song to its specific information by creating a class and then write this out. You may wish to write it as binary as an ObjectStream of sorts, or if this list is going to grow or get any more complex, then a database is what you need. But more importantly,
    I have no Java training, and learn only by fiddling and trial and error, and my minimal ability has not allowed me to figure out how to export my String arrays to text files.
    why stumble around like a drunk man in the dark when you don't have to? Do yourself a very big favor by going to the Sun Java tutorials, and learn Java from the ground up. You'll be glad that you did.

    Good luck.

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Hehe, okay, I have gone through parts of the tutorial quite often for help, especially the first parts, with the serious fundamentals.

    Maybe I didn't explain the situation perfectly. I won't be needing a database for quite a while, as I intend to keep it quite simple. I don't suit understand what you're saying about tying the song to it's information. For example, if my 6th song in the Library is by Pink Floyd, and I access this: lib[5][1], 'Pink Floyd' will be printed out. So it would be a simple matter to use a for loop to print out all the details from this song, i.e. lib[5][0] will print out the name of the song.

    To simplify it, could you perhaps just write out a code segment to save 'Hello World' to a text file. I can easily work out the rest from there.

    Thanks very much.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Search for BufferedWriter or FileWriter or FileOutputStream for sample code that writes to a file.

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Java Code:
    BufferedWriter f = new BufferedWriter(new FileWriter(path+name));
    f.write("thingy");
    f.newLine();
    f.write("another thingy");
    f.flush();
    f.close();
    package java.io
    I die a little on the inside...
    Every time I get shot.

  6. #6
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Great thanks, that looks good.

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    You're welcome.
    I die a little on the inside...
    Every time I get shot.

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by carderne View Post
    I have a String array like this: String[400][4]
    This allows for 400 songs, with 4 bits of information about each.
    Now that you have the basics working, you really should change your code. You are using an array of 400 elements, each with a String[4] as its element.

    You should:
    1) use an ArrayList or other structure that allows dymanic growth
    2) use a class (or structure) to hold the four elements with names and strong typing.

    Its much better form to have a class, say PlayListElement defined with named fields for URL to the song, artist, genre, etc.

  9. #9
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    I thought that the array method I'm using was a bit cumbersome.

    It obviously means I can only have 400 (or whatever size I make the array) songs.

    So I can use arraylist to make it dynamic. And I don't follow your second suggestion though, about strong typing etc. Would you mind clarifying a bit? I'll have a look this weekend and try your suggestion about using an ArrayList.

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Create a class (either external in a file, or internal to the class) something like this:

    Java Code:
    class SongStruct {
        String title;
        Date stamp;
        String songURL;
        String genre
            /**
             * Construct an instance of SongStruct
             */
        SongStruct(String t Date t, String u, String g) {
            title - t;
            stamp = t;
            songURL = u;
            genre = g;
        }
        String getTitle() { return title;}
        String getURL() { return songURL;}
        String getGenre() { return genre;}
        String timeAsString() {
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            return df.format(stamp);
        }
    }
    Then you can use this as the entries in your ArrayList

    ArrayList<SongStruct> theList = new ArrayList<SongStruct>();
    Last edited by fishtoprecords; 09-10-2008 at 04:27 PM. Reason: fix misnamed construtor

  11. #11
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Ok thanks. I'll be able to try that out this weekend and I'll post back to let you know how it goes.

  12. #12
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default strong typing

    Quote Originally Posted by carderne View Post
    ......don't follow your second suggestion though, about strong typing etc. Would you mind clarifying a bit? I'll have a look this weekend and try your suggestion about using an ArrayList.
    It is a manner of speaking that derives from some languages allowing one to program without great detail. Putting data in a class and stating what the data is allows clearer thinking as to what the data does.

    Java uses that model.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  13. #13
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    It is a manner of speaking that derives from some languages allowing one to program without great detail. Putting data in a class and stating what the data is allows clearer thinking as to what the data does.

    Java uses that model.
    Ok, thanks.
    Thanks for your pointers everyone. I'll put that new knowledge to test this weekend.

  14. #14
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    I did the export thing, which was easy easy enough, and working on the ArrayList, but I don't know how to extract the information... This is what I've done so far:

    Java Code:
    class SongStruct {
        String name;
        String artist;
        String album;
    
        SongStruct(String na, String ar, String al) {
            name = na;
            artist = ar;
            album = al;
        }
    }
    Java Code:
    class ArrayListTest {
        public static void main(String[] args) {
            ArrayList<SongStruct> theList = new ArrayList<SongStruct>();
            theList.add( new SongStruct("lay lady lay", "bob dylan", "best of") );
        }
    }
    So I've verified that that information has been added. But now I want to access the data.

    I've tried to get the data by using this:
    Java Code:
    theList.toArray();
    But that returns an array of Objects. How can I get these Objects as Strings, and I've tried casting...

  15. #15
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    a cannonical for each loop would be the construct of choice, to put them in a text file use a PrintWriter or FileWriter...

    If you want just one, you will have to have an indexing, which can be a Hash of some kind - probably HashSet.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  16. #16
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    I still don't quite follow, but let me show my code to better explain the situation: (I've simplified it a lot to fit it in)

    Java Code:
    class SongStruct {
    	String song;
    	SongStruct(String s) {
            	song = s;
    	}
    }
    Java Code:
    class ArrayListTest {
    	ArrayList<SongStruct> songList = new ArrayList<SongStruct>();
    	ArrayListTest() {}
    	void addSong(String s) {
    		songList.add(new SongStruct(s));
    	}
    }
    This method is run when I click a button in my GUI:
    Java Code:
    private void btnActionPerformed(java.awt.event.ActionEvent evt) {                                       
    	ArrayListTest.addSong(txtFieldSong.getText());
    	output.setListData(ArrayListTest.songList.toArray());
    }
    This is what is displayed in the list: SongStruct@1050169

  17. #17
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    I know why it won't work. It's currently trying to print out what is essentialy an object of the SongStruct class. But I can I extract this information while referencing the ArrayList stuff?

  18. #18
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Quote Originally Posted by carderne View Post
    This method is run when I click a button in my GUI:
    Java Code:
    private void btnActionPerformed(java.awt.event.ActionEvent evt) {                                       
    	ArrayListTest.addSong(txtFieldSong.getText());
    	output.setListData(ArrayListTest.songList.toArray());
    }
    songList.getItemAt( posittion );

    Which will require looking up syntax for ArrayList indexing and retrieval, what we get around to is what are you keying the list on?

    Makes a lot of difference, not immediately apparent how to do it but is relatively simple.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  19. #19
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Ok, I'll try getItemAt() but that still returns an Object doesn't it? How do I get that information out of the object?

    "keying the list on"
    What does that mean?

  20. #20
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    If you reference your list of SongStruct, it should return a SongStruct object, not a generic Object.

    Not clear if your SongStruct is in the same file or not, or what the access restrictions are. Typically one adds getter functions to access files.
    Java Code:
    class SongStruct { 
               String song; 
         String artist;
         SongStruct(String s) { 
              song = s; 
              artist = "The Beatles";
    }
         String getSong() { return song;}
         String getArtist() {
                 return artist;
         }
    }
    
    ArrayList<SongStruct> songList = new ArrayList<SongStruct>(); 
    // add a bunch of songs to songList
    
    SongStruct aSong = songList.get(2);
    System.out.println(aSong.getSong());
    System.out.println(aSong.getArtist());
    You may need to learn more about basic object creation, members and access (usually called getters and setters).

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  2. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  3. Replies: 3
    Last Post: 08-05-2008, 03:19 AM
  4. problem with the way to export data to CSV file
    By gonne in forum Java Servlet
    Replies: 0
    Last Post: 07-01-2007, 02:43 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
  •