Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read, translate to hex, write?

 
Will Carpenter
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read a character from an input file, translate it to hex and write the hex characters to an output file. Any suggestions?

What I have so far
import java.io.*;
class TrivialApplication
{ public static void main(String args[])
{
int i;
String inf = "p3in.txt";
String ouf = "pout.txt";
FileInputStream fin;
FileOutputStream fou;

try
{ fin = new FileInputStream(inf);//set up input file
}
catch(FileNotFoundException fnf)
{ System.out.println(inf);
return;
}

try
{ fou = new FileOutputStream(ouf);//set up output file
}
catch(FileNotFoundException fnf)
{ System.out.println(fnf);
return;
}

try
{
do
{
i = fin.read(); //read a char in; write a char out
if(i != -1)
{
???;

fou.write(?);
}
}
while(i != -1);
}
catch(IOException ioe)
{ System.out.println("File error");
}
fin.close();
fou.close();
}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the character as an int. Look at the Integer class and see if there isn't a method just made for you.
 
Will Carpenter
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Stan; but I tried
String s = Integer.toHexString(i);
already, but when I tried
fou.write(s);
that didn't work, because write() won't take a string as an arg;
is there a method that returns a hex as anything other than a String?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the methods in java.io.PrintWriter
 
Will Carpenter
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike!
I changed all of my FileOutputStreams to FileWriters (and FileInputStreams to FileReaders) and that gave me access to a write() method that writes Strings!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic