Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java File IO 1

Status
Not open for further replies.

crmayer

Programmer
Nov 22, 2002
280
US
I have created a class that reads in a file, when I test it internally, it works fine (with a file that I manually created). So I got the file that I want to read in via FTP and now I am getting the "white space" symbol between each character. If I open the file that we get via FTP and copy and paste the contents into a new .txt file, it reads it in fine.
My guess is that is has something to do with the file format, but I really do not know. Nor am I finding anything on this via internet.

The link will show you the two sets of results. The top one is the bad results and lower one are the good results.

Thanks in advance....
 
Sorry, not sure if this is relevent or not, but the file that I ftp is about 60KB, and when I copy and past the contents of this file to a new txt file, it is only about 30KB.
 
Look at your file in Hex View, e.g. in Notepad is it in menu Options/Hex.
I guess that every second character in your file is hex 00 character.
 
I do not have an "Options" in my Notepad....
 
OK, I did download a hex viewer and you are right, every other character is 00...

Is there a way to not read that in via java?
 
Oops, this was not Notepad, this is a viewer from Total Commander :)
I never done it in Java, but look at any function than can replace a character in the string with another character, or do it via regular expression.

In REXX, Perl an Python it would be trivial e.g. this REXX-code:
Code:
nullhex  ="00"x
mystring ="H"||nullhex||"e"||nullhex||"l"||nullhex||"l"||nullhex||"o"
say "mystring = '"||mystring||"'"
/* removing "00"x from mystring */
mystring=changestr(nullhex, mystring, "")
say "mystring = '"||mystring||"'"
does this work:
Code:
mystring = 'H e l l o'
mystring = 'Hello'

So I am sure, that in Java would it be trivial too :)
 
How are you FTP'ing (bin or asc)? And try the other way.

_________________
Bob Rashkin
 
I'll bet the FTP'd file is being transfered as unicode (16-bit chars), whether or not that's true of the file itself before FTP. That might be something you can affect either in the transfer or in the reader.

_________________
Bob Rashkin
 
If I look at the file on the ftp site via explorer, it shows the same size as the "bad" file on my side....

I am not sure what to do with it from here.

They claim that the file is OK on their end....
 
If I'm right about the unicode thing, you may need to play with the string class to convert.

_________________
Bob Rashkin
 
HEX '00' is a string termination character ('\0') used in C/C++.
To remove it from a text file I would read the text file line by line and remove the unwanted character from every line using one of these 2 alternatives:
Code:
package examples;

import java.util.regex.*;

public class RemoveHex {
  public static void main(String args[]){
    char hexnull = 0x00;
    String mystring;
    
    //using String.replace() method    
    System.out.println("Using String.replace() method:");
    mystring = "H"+hexnull+"e"+hexnull+"l"+hexnull+"l"+hexnull+"o";
    System.out.println("mystring ='" + mystring +"'");
    mystring = mystring.replace("\0", "");
    System.out.println("mystring ='" + mystring +"'");
    
    //using regexp
    System.out.println("Using java.util.regex.Matcher.replaceAll() method:");
    mystring = "H"+hexnull+"e"+hexnull+"l"+hexnull+"l"+hexnull+"o";
    System.out.println("mystring ='" + mystring +"'");
    Pattern p = Pattern.compile("\0");
    Matcher m = p.matcher(mystring);
    String result = m.replaceAll("");
    System.out.println("result ='" + result +"'");
  }
}
The result is:
Code:
Using String.replace() method:
mystring ='H e l l o'
mystring ='Hello'
Using java.util.regex.Matcher.replaceAll() method:
mystring ='H e l l o'
result ='Hello'
 
That did the trick.....
I have to change the String.replace one a little bit, but I got them both working and I am now getting the results that I need/want.

Thanks you so much.........

How can I repay you?


Thanks again.
 
Hi crmayer,
I was glad to help you without repayment.
But you can give me star :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top