473,698 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange DataOutputStrea m file.

2 New Member
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:

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 3111
vipersniper5
9 New Member
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!
Dec 9 '07 #2
JosAH
11,448 Recognized Expert MVP
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 New Member
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 InputStreamRead er(java.lang.St ring)
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 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:
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 Recognized Expert Top Contributor
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:

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

Look up InputStreamRead er 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
2714
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 fashion as DataInputStream and DataOutputStream. I am ambivalent as to whether I should: (a) subclass DataInputStream / DataOutputStream and override what is needed (b) subclass FilterInputStream / FilterOutputStream and implement the
0
2031
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 NetBeans. I utilize the javax.xml.transform class and in the computer where I wrote the program all function perfectly, calling the input XML file, the XSLT stylesheets and setting where to write the output HTML.
0
1533
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 are written in C# The server is quite simple: two threads, one accepts new connections and decides whether the client is authenticated to send data or not, and the other thread serves already connected clients: performs a Socket.Select and then...
24
1718
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: Dim xSng As Single = 6547.972 Dim yInt As Integer = 8000 Dim num As Integer = CInt(Math.Floor(xSng * yInt)) Dim numMinusOne As Integer = CInt(Math.Floor(xSng * yInt) - 1)
6
2359
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 problem also with other imports (e.g. urllib2 etc.). And again very often in all these cases where I get weired Python exceptions, the problem is around re-functions - usually during re.compile calls during import (see some of the exceptions below). But...
4
4900
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. The problem is that I get rejected rows because of this. Best regards,
2
1814
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 would be in this XML file is a String property that contains the code for a VBScript. I have a separate form just for editing this script that uses a single Textbox. This Textbox has the AcceptsReturn, AcceptsTab and Multiline properties all set...
1
17639
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 BufferedOutputStream objects. i am newbie to java, i will appreciate if the explanation is clear enough for a beginner to understand e.g: DataOutputStream out2 = new DataOutputStream( new BufferedOutputStream(new FileOutputStream("Data.txt")) );
8
5310
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. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8895
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7728
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.