Strange DataOutputStream file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Asylus
    New Member
    • Dec 2007
    • 2

    #1

    Strange DataOutputStream file.

    Thank you in advance for any help you may be able to provide.

    I am trying to create a file using DataOutputStrea m. I'm using the following code to write the file:

    Code:
    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++)
    					[B]outputStream.writeChar((int)artistAlbum[i][j].charAt(k)[/B]);
    				}
    				[B]outputStream.writeChar('\n')[/B];
    			}
    		}
    		outputStream.close();
    		System.out.println("");
    		System.out.println(dataB + " Written");
    	}
    I've used both writeChar() and writeUTF() for the strings, but neither seems to work properly.

    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:

    Code:
    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;
        }
    Which makes a call to:

    Code:
    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 code can read in the newly written code- BUT it doesn't detect all of the blank lines, leaving me with an incorrect input when the file is reread.

    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?
  • vipersniper5
    New Member
    • Dec 2007
    • 9

    #2
    First as to the source of you problem:
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream .html
    the readline method had been deprecated specifically because of it does properly read in characters written by an DataOutputStrea m(this is news to me, but then I've never used one). The link provided also provides the solution of using the buffered readline method as follows:

    Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
    DataInputStream d = new DataInputStream (in);

    with:
    BufferedReader d = new BufferedReader( new InputStreamRead er(in));

    Frankly though, just from glancing through your code, DataInput/Output streams don't seem like the way to go, I'd highly recommend going to BufferedReader/Writer s. If you must have a binary file, you can still do it with the BufferedRead/Writer s by using a InputStreamWrit er in the constructor. As far as I can tell DataOutput streams give no advantage over this approach. (though I could be wrong)
    Good luck!

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Data Streams are for binary data. The result you see in your file are the two byte
      (big endian) representations for chars (^@ is a 0x00 byte). As has been written
      before use Readers and Writers instead, they handle the conversion to/from two
      byte unicode codepoints behind your back perfectly well.

      kind regards,

      Jos

      Comment

      • Asylus
        New Member
        • Dec 2007
        • 2

        #4
        Thanks for the help so far; but one other problem popped up;

        I switched over to the Buffered reader:
        Code:
        BufferedReader inputStream = new BufferedReader(new InputStreamReader(dataB));
        But when I compile I get the error:
        cannot find symbol constructor InputStreamRead er(java.lang.St ring)
        My import list is as follows:
        Code:
        import java.io.*;
        import java.lang.*;
        import java.util.*;
        
        import java.lang.Object;
        import java.io.Reader;
        import java.io.BufferedReader;
        import java.io.InputStreamReader;
        These last four were added in after I had trouble with the InputStreamRead er (and if I switch InputStreamRead er over to DataInputStream- it gives the exact same error as above except instead of InputStreamRead er it decided it can't find BufferedReader) .

        Also; previously I needed to initialize this for my readers to be usable:
        Code:
        FileInputStream midman = new FileInputStream(dataB);
        Is there anything I should do with this part of the code?

        Thanks again in advance for any help you can provide!

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          An error like that is telling you that you are trying to use a constructor that doesn't exist. There are no strict patterns to constructors: just because DataInputStream has a constructor that takes a string doesn't imply there is a matching constructor in InputStreamRead er.

          The most important thing for you to learn to use is the API documentation:



          Look up InputStreamRead er and you will see all its constructors.

          Comment

          Working...