473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Strange DataOutputStream file.

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

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

Expand|Select|Wrap|Line Numbers
  1. public static void rewriteFile(String[][] artistAlbum) throws IOException
  2.     {
  3.         System.out.println("Writing file: " + dataB);
  4.         DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(dataB));
  5.         FileOutputStream midman = new FileOutputStream(dataB);
  6.  
  7.         for(int i=0;i<artistAlbum.length;i++)
  8.         {
  9.             for(int j=0;j<MAX_SONGS_PER_ALBUM;j++)
  10.             {
  11.                 if(artistAlbum[i][j].equals(END))
  12.                 {j=MAX_SONGS_PER_ALBUM;}
  13.                 else
  14.                 {
  15.                     for(int k=0;k<artistAlbum[i][j].length();k++)
  16.                     outputStream.writeChar((int)artistAlbum[i][j].charAt(k));
  17.                 }
  18.                 outputStream.writeChar('\n');
  19.             }
  20.         }
  21.         outputStream.close();
  22.         System.out.println("");
  23.         System.out.println(dataB + " Written");
  24.     }
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:

Expand|Select|Wrap|Line Numbers
  1. public static String[][] getList() throws IOException
  2.     {
  3.         int[] lineCount = new int[4];
  4.         lineCount = countLines();
  5.         DataInputStream inputStream = new DataInputStream(new FileInputStream(dataB));
  6.         FileInputStream midman = new FileInputStream(dataB);
  7.  
  8.         int numSections = lineCount[1];
  9.         if (lineCount[2] == 1)
  10.         {numSections--;}
  11.         if (lineCount[3] == 0)
  12.         {numSections++;}
  13.  
  14.         String[][] artistAlbum = new String[numSections][MAX_SONGS_PER_ALBUM];
  15.         String line = "";
  16.         int section = 0;
  17.         int secLine = 0;
  18.         for(int i=1;i<=lineCount[0];i++)
  19.         {
  20.             line = inputStream.readLine();
  21.             if (i == lineCount[3]) //i will never = 0, so lineCount[3] (which can only be 1 or 0)
  22.             {lineCount[3] = 0;}        //will match only if we need to skip.
  23.             else if (i == lineCount[0] && lineCount[2] == 0)
  24.             {
  25.                 artistAlbum[section][secLine] = line;
  26.                 secLine++;
  27.                 artistAlbum[section][secLine] = END;
  28.             }
  29.             else if (line.equals(""))
  30.             {
  31.                 artistAlbum[section][secLine] = END;
  32.                 section++;
  33.                 secLine = 0;
  34.             }
  35.             else
  36.             {
  37.                 artistAlbum[section][secLine] = line;
  38.                 secLine++;
  39.             }
  40.         }
  41.         inputStream.close();
  42.  
  43.         return artistAlbum;
  44.     }
Which makes a call to:

Expand|Select|Wrap|Line Numbers
  1. public static int[] countLines() throws IOException
  2.     {
  3.         DataInputStream inputStream = new DataInputStream(new FileInputStream(dataB));
  4.         FileInputStream midman = new FileInputStream(dataB);
  5.  
  6.         int numLines = 0;
  7.         int numBlanks = 0;
  8.         int TFLastBlank = 0; //True or False: Is the last line a blank?
  9.         int TFFirstBlank = 0;//True or False: Is the first line a blank?
  10.         String line = "FirstRun";
  11.         String store = "FirstRun";
  12.         do{
  13.             store = line;
  14.             line = inputStream.readLine();
  15.             if (line != null)
  16.             {
  17.                 numLines++;
  18.                 if (line.equals(""))
  19.                 {numBlanks++;}
  20.                 if (line.equals("") && store.equals("FirstRun"))
  21.                 {TFFirstBlank = 1;}
  22.             }
  23.             if (line == null && store.equals(""))
  24.             {TFLastBlank = 1;}
  25.         }while (line != null);
  26.         int[] lineCount = new int[4];
  27.         lineCount[0] = numLines;
  28.         lineCount[1] = numBlanks;
  29.         lineCount[2] = TFLastBlank;
  30.         lineCount[3] = TFFirstBlank;
  31.         inputStream.close();
  32.         return lineCount;
  33.     }
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?
Dec 9 '07 #1
4 3097
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 DataOutputStream(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 InputStreamReader(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 InputStreamWriter in the constructor. As far as I can tell DataOutput streams give no advantage over this approach. (though I could be wrong)
Good luck!
Dec 9 '07 #2
JosAH
11,448 Expert 8TB
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
Dec 9 '07 #3
Asylus
2
Thanks for the help so far; but one other problem popped up;

I switched over to the Buffered reader:
Expand|Select|Wrap|Line Numbers
  1. BufferedReader inputStream = new BufferedReader(new InputStreamReader(dataB));
But when I compile I get the error:
cannot find symbol constructor InputStreamReader(java.lang.String)
My import list is as follows:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.lang.*;
  3. import java.util.*;
  4.  
  5. import java.lang.Object;
  6. import java.io.Reader;
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
These last four were added in after I had trouble with the InputStreamReader (and if I switch InputStreamReader over to DataInputStream- it gives the exact same error as above except instead of InputStreamReader it decided it can't find BufferedReader).

Also; previously I needed to initialize this for my readers to be usable:
Expand|Select|Wrap|Line Numbers
  1. 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!
Dec 16 '07 #4
BigDaddyLH
1,216 Expert 1GB
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 InputStreamReader.

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

http://java.sun.com/javase/6/docs/api/

Look up InputStreamReader and you will see all its constructors.
Dec 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alexandros Karypidis | last post by:
Hi all, I would like some advice regarding the following design issue: I would like to be able to exchange data in XDR format. I was thinking of writing a pair of I/O streams in the same...
0
by: Federico | last post by:
Hi all, I don't know if this topic is perhaps a little bit off-topic, anyway I have a strange problem in transforming an XML file in an HTML file using XSLT form a Java program written with...
0
by: Grzegorz Kaczor | last post by:
Hello all, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients...
24
by: David | last post by:
hello. when doing the simple following computation, the value put into the variable numMinusOne is NOT the same as what the computation is showed to be in the Watch window!! here is the code:...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
4
by: Gregor KovaĨ | last post by:
Hi! When I'm using IMPORT with INSERT_UPDATE I sometimes get SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table. I'm not sure why this happens....
2
by: zacks | last post by:
I am developing an app in VS2005 (actually in VB.NET but this question, I believe, would apply to any .NET language) that is used to design the contents of an XML file. One of potential items that...
1
by: stmfc | last post by:
generally i see that BufferedOutputStream objects are wrapped with DataOutputStream, what is the reason for this design? why do coders prefer this style instead of doing operations on...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.