I am trying to create a file using DataOutputStream. I'm using the following code to write the file:
Expand|Select|Wrap|Line Numbers
- public static void rewriteFile(String[][] artistAlbum) throws IOException
- {
- System.out.println("Writing file: " + dataB);
- DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(dataB));
- FileOutputStream midman = new FileOutputStream(dataB);
- for(int i=0;i<artistAlbum.length;i++)
- {
- for(int j=0;j<MAX_SONGS_PER_ALBUM;j++)
- {
- if(artistAlbum[i][j].equals(END))
- {j=MAX_SONGS_PER_ALBUM;}
- else
- {
- for(int k=0;k<artistAlbum[i][j].length();k++)
- outputStream.writeChar((int)artistAlbum[i][j].charAt(k));
- }
- outputStream.writeChar('\n');
- }
- }
- outputStream.close();
- System.out.println("");
- System.out.println(dataB + " Written");
- }
What I want to get out is:
Bob Dylan
1966 Blonde on Blonde
-Rainy Day Women #12 & 35
-Pledging My Time
-Visions of Johanna
Led Zeppelin
1969 II
-Whole Lotta Love
-What Is and What Should Never Be
-The Lemon Song
-Thank You
Each line in the output is a single piece from artistAlbum[][].
What I do get is:
^@B^@o^@b^@ ^@D^@y^@l^@a^@n^@
^@1^@9^@6^@6^@ ^@B^@l^@o^@n^@d^@e^@ ^@o^@n^@ ^@B^@l^@o^@n^@d^@e^@
^@-^@R^@a^@i^@n^@y^@ ^@D^@a^@y^@ ^@W^@o^@m^@e^@n^@ ^@#^@1^@2^@ ^@&^@ ^@3^@5^@
^@-^@P^@l^@e^@d^@g^@i^@n^@g^@ ^@M^@y^@ ^@T^@i^@m^@e^@
^@-^@V^@i^@s^@i^@o^@n^@s^@ ^@o^@f^@ ^@J^@o^@h^@a^@n^@n^@a^@
^@
^@L^@e^@d^@ ^@Z^@e^@p^@p^@e^@l^@i^@n^@
^@1^@9^@6^@9^@ ^@I^@I^@
^@-^@W^@h^@o^@l^@e^@ ^@L^@o^@t^@t^@a^@ ^@L^@o^@v^@e^@
^@-^@W^@h^@a^@t^@ ^@I^@s^@ ^@a^@n^@d^@ ^@W^@h^@a^@t^@ ^@S^@h^@o^@u^@l^@d^@ ^@N^@e^@v^@e^@r^@ ^@B^@e^@
^@-^@T^@h^@e^@ ^@L^@e^@m^@o^@n^@ ^@S^@o^@n^@g^@
^@-^@T^@h^@a^@n^@k^@ ^@Y^@o^@u^@
^@
As you can see- not the prettiest sight. writeUTF had a few less, but they were all different. You had ^@ ^% ^$omething else and ^@nother thing. Scattered much more randomly.
Now just looking at the thing I don't care about, but what my code is trying to do will allow me to read in the data, modify it, then write it back to the same file and be able to read it again. The file has to start off clean, as shown in the first output example.
My code to read in the data:
Expand|Select|Wrap|Line Numbers
- public static String[][] getList() throws IOException
- {
- int[] lineCount = new int[4];
- lineCount = countLines();
- DataInputStream inputStream = new DataInputStream(new FileInputStream(dataB));
- FileInputStream midman = new FileInputStream(dataB);
- int numSections = lineCount[1];
- if (lineCount[2] == 1)
- {numSections--;}
- if (lineCount[3] == 0)
- {numSections++;}
- String[][] artistAlbum = new String[numSections][MAX_SONGS_PER_ALBUM];
- String line = "";
- int section = 0;
- int secLine = 0;
- for(int i=1;i<=lineCount[0];i++)
- {
- line = inputStream.readLine();
- if (i == lineCount[3]) //i will never = 0, so lineCount[3] (which can only be 1 or 0)
- {lineCount[3] = 0;} //will match only if we need to skip.
- else if (i == lineCount[0] && lineCount[2] == 0)
- {
- artistAlbum[section][secLine] = line;
- secLine++;
- artistAlbum[section][secLine] = END;
- }
- else if (line.equals(""))
- {
- artistAlbum[section][secLine] = END;
- section++;
- secLine = 0;
- }
- else
- {
- artistAlbum[section][secLine] = line;
- secLine++;
- }
- }
- inputStream.close();
- return artistAlbum;
- }
Expand|Select|Wrap|Line Numbers
- public static int[] countLines() throws IOException
- {
- DataInputStream inputStream = new DataInputStream(new FileInputStream(dataB));
- FileInputStream midman = new FileInputStream(dataB);
- int numLines = 0;
- int numBlanks = 0;
- int TFLastBlank = 0; //True or False: Is the last line a blank?
- int TFFirstBlank = 0;//True or False: Is the first line a blank?
- String line = "FirstRun";
- String store = "FirstRun";
- do{
- store = line;
- line = inputStream.readLine();
- if (line != null)
- {
- numLines++;
- if (line.equals(""))
- {numBlanks++;}
- if (line.equals("") && store.equals("FirstRun"))
- {TFFirstBlank = 1;}
- }
- if (line == null && store.equals(""))
- {TFLastBlank = 1;}
- }while (line != null);
- int[] lineCount = new int[4];
- lineCount[0] = numLines;
- lineCount[1] = numBlanks;
- lineCount[2] = TFLastBlank;
- lineCount[3] = TFFirstBlank;
- inputStream.close();
- return lineCount;
- }
This is the last part of the code I need to get done, but I am unsure as to how to proceed with this part, since no matter how I write the file I get these weird symbols.
Anyone have any suggestions?