How to get only characters from a file in java [duplicate] - java

This question already has answers here:
extract data column-wise from text file using Java
(2 answers)
Closed 4 years ago.
I have a file txt. This is the file:
Team P W L D F A Pts
1. Arsenal 38 26 9 3 79 - 36 87
2. Liverpool 38 24 8 6 67 - 30 80
3. Manchester_U 38 24 5 9 87 - 45 77
4. Newcastle 38 21 8 9 74 - 52 71
5. Leeds 38 18 12 8 53 - 37 66
6. Chelsea 38 17 13 8 66 - 38 64
7. West_Ham 38 15 8 15 48 - 57 53
8. Aston_Villa 38 12 14 12 46 - 47 50
9. Tottenham 38 14 8 16 49 - 53 50
How can I get only the name of teams? I tried to use the regex in the following way but don't work:
FileReader f;
f=new FileReader("file.txt");
BufferedReader b;
b=new BufferedReader(f);
s=b.readLine();
String[] name = s.split("\\w+");
for(int i=0;i<name.length;i++)
System.out.println(name[i]);
How do I solve? Thanks to everyone in advance!

FileReader f;
f=new FileReader("file.txt");
BufferedReader b;
b=new BufferedReader(f);
while(s=b.readLine()!=null){
Matcher name=Pattern.compile("(?<=\\d\\.\\s)\\S+").matcher(s);
if(name.find())
System.out.println(name.group());
}
here the regex (?<=\\d\\.\\s)\\S+ will match only the name after the serial no. Regex

If you want to read line by line and your file has structure as you presented. These code enable you to get clubs names.
File f = new File("file.txt");
Scanner sc = new Scanner(f);
sc.nextLine();
while (sc.hasNextLine()) {
String[] name = sc.nextLine().split("\\s+");
System.out.println(name[1]);
}

try to use replaceAll, find all word characters (a-zA-Z_) and replace them all with empty. this gives team name.
s=b.readLine();
s.replaceAll("[^a-zA-Z_]+","");
System.out.println(s);

Your string s is one line:
1. Arsenal 38 26 9 3 79 - 36 87
All you need to do is split by space and get second entry:
s.split(" ")[1]
RegEx is overkill here. Do this for each line and add the name to a list at each step.

Related

What is the parseBase64Binary function in Python?

In Java, I use:
String str = “%EF!c&WrDwCCTe<fX$,#8L<YTs?G5d>F])ub.63G=Xn<cdef2R{47JQexxN”;
byte[] result = DatatypeConverter.parseBase64Binary(str);
for(byte i : result){
System.out.print(i);
System.out.print(" ");
}
to decode str.
Output:
16 87 22 -84 60 2 9 55 -97 95 -62 -40 78 -63 -71 116 91 -101 -21 113 94 119 29 121 -3 -111 -29 -78 80 123 28 77
Now I need to decode str with Base64 in python but I don't know which lib and function should I choose.
I've tried base64.b64decode but its result is different from that in Java.
str = '%EF!c&WrDwCCTe<fX$,#8L<YTs?G5d>F])ub.63G=Xn<cdef2R{47JQexxN'
result = base64.b64decode(str)
print(result)
for i in range(0, len(decode_secret)):
print(decode_secret[i], end=" ")
Output:
b'\x10W\x16\xac<\x02\t7\x9f_\xc2\xd8N\xc1\xb9t[\x9b\xebq'
16 87 22 172 60 2 9 55 159 95 194 216 78 193 185 116 91 155 235 113

for loop iteration cant find whats making i jump values

FILE THATS BEING READ
Rob Gronkowski 48
Zach Ertz 34
Travis Kelce 29
Evan Engram 15
Jimmy Graham 12
Cameron Brate 10
Delanie Walker 9
Kyle Rudolph 6
Austin Seferian-Jenkins 6
Jack Doyle 6
Hunter Henry 5
Jason Witten 4
Jordan Reed 4
Vernon Davis 3
Jared Cook 3
Tyler Kroft 3
Ed Dickson 3
Charles Clay 3
George Kittle 3
Antonio Brown 67
DeAndre Hopkins 62
A.J. Green 62
Mike Evans 62
Julio Jones 56
Michael Thomas 55
Dez Bryant 53
Michael Crabtree 45
Brandin Cooks 42
Tyreek Hill 42
Doug Baldwin 42
Keenan Allen 32
Jarvis Landry 29
Will Fuller 29
Amari Cooper 29
Stefon Diggs 29
Alshon Jeffery 27
Nelson Agholor 24
Adam Thielen 24
Chris Hogan 24
Golden Tate 24
Demaryius Thomas 22
Jordy Nelson 22
Larry Fitzgerald 22
DeSean Jackson 21
JuJu Smith-Schuster 19
Devante Parker 18
Devin Funchess 18
Kelvin Benjamin 18
T.Y. Hilton 17
Emmanuel Sanders 17
Marvin Jones 15
Rishard Matthews 14
Pierre Garcon 14
Cooper Kupp 14
Sterling Shepard 14
Paul Richardson 11
Danny Amendola 10Le’Veon Bell 70
Kareem Hunt 63
Todd Gurley 63
Leonard Fournette 60
Melvin Gordon 60
LeSean McCoy 60
Mark Ingram 50
Devonta Freeman 50
Jordan Howard 50
Lamar Miller 41
Doug Martin 34
Carlos Hyde 34
Aaron Jones 27
Alvin Kamara 27
Jerick McKinnon 24
DeMarco Murray 21
Chris Thompson 21
Jay Ajayi 21
Joe Mixon 18
C.J. Anderson 17
Tevin Coleman 17
Christian McCaffrey 17
Derrick Henry 16
Alex Collins 16
Dion Lewis 15
Adrian Peterson 13
Duke Johnson 12
Marshawn Lynch 11
Ameer Abdullah 10
Bilal Powell 9
LeGarrette Blount 9
Marlon Mack 9
James White 8
Ezekiel Elliott 7
Latavius Murray 7
Frank Gore 7
Isaiah Crowell 7
Orleans Darkwa 7
Kenyan Drake 5
Matt Forte 5
Darren McFadden 5
Alfred Morris 5
Damien Williams 3
Tarik Cohen 3
Jonathan Stewart 3
Robert Kelley 3
Danny Woodhead 3
Ty Montgomery 2
Javorius Allen 2
Mike Gillislee 2
Thomas Rawls 2
Theo Riddick 2
DeAndre Washington 2
Eddie Lacy 2
Giovani Bernard 2
Andre Ellington 2
Austin Ekeler 2
Jalen Richard 2
Ted Ginn 10
Robby Anderson 10
Jermaine Kearse 9
Davante Adams 9
Kenny Stills 9
Sammy Watkins 9
Marqise Lee 5
Mohamed Sanu 5
Allen Hurns 5
Josh Doctson 5
Jamison Crowder 4
Jeremy Maclin 3
Randall Cobb 3
Tyrell Williams 3
Robert Woods 3
Corey Davis 3
Jordan Matthews 3
Tyler Lockett 3
John Brown 2
Willie Snead 2
Donte Moncrief 2
Deshaun Watson 31
Dak Prescott 26
Tom Brady 24
Russell Wilson 22
Drew Brees 22
Carson Wentz 20
Alex Smith 14
Kirk Cousins 13
Matthew Stafford 11
Marcus Mariota 11
Tyrod Taylor 11
Cam Newton 11
Matt Ryan 11
Philip Rivers 8
having some problems been looking all over for answers. I found out my for loop iteration is incorrect it prints the series:0,1,2,10 etc. I was wondering if someone can point out my flaw, so I can fix this. I apprectiate anyone reading this, and appolgozie for the length of code. But just wanted to include everything so I don't miss anything. FOR LOOP LINE 87 thanks again, sincerely java noob
CODE
package trades;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;
public class Fantasy {
public static void main(String[] args) {
int[] playerRanking = new int[75];
String infoComingIn = null;
//Finding file path
String filename = "C:\\Users\\Karanvir\\Desktop\\21days\\players.txt";
File filez = new File(filename);
BufferedReader br;
String[] playerNames = new String[75];
int counterOfReadLines = 0;
Pattern p = Pattern.compile("[0-9]{2,3}");
ArrayList<Integer> arrayList = new ArrayList<Integer>();
try {
br = new BufferedReader(new FileReader(filez));
playerNames[counterOfReadLines] = br.readLine();
while (br.readLine() != null) {
counterOfReadLines = counterOfReadLines + 1;
playerNames[counterOfReadLines] = br.readLine();
System.out.println(playerNames[counterOfReadLines - 1]);
}
br.close();
for (int i = 0; i < playerNames.length; i++) {
Matcher m = p.matcher(playerNames[i]);
if (m.find()) {
String matched = m.group(0);
int addToArray = Integer.parseInt(matched);
playerRanking[i] = addToArray;
System.out.println(i);
}
}
} catch (Exception e) {}
}
}
Okay, so by seeing the post, I can point out only one issue. Since you are incrementing counterOfReadLines variable before the line
playerNames[counterOfReadLines] = br.readLine();
so what happens is playerNames is initializing with the array of index 1 not 0 and when you are trying to call the loop below:-
for (int i = 0; i < playerNames.length; i++) {
Matcher m = p.matcher(playerNames[i]);
if (m.find()) {
String matched = m.group(0);
int addToArray = Integer.parseInt(matched);
playerRanking[i] = addToArray;
System.out.println(i);
}
it is incrementing with 0. So either start it from i=1 or increment the counterOfReadLines after the line
playerNames[counterOfReadLines] = br.readLine();
so your error will go away...! if not let me know... :) !

Different data when printing and writing to file

There is a stream of data which is sent from server. I need to store this byte stream into a file. The problem is the data which I output to console and the one which I store in a file are different. Seems like there is a change in format of data when I stored in a file.
Here is the program:
try
{
System.out.println("My Address is "+serverSocket.getLocalSocketAddress());
Socket server = serverSocket.accept(); // return a new socket
System.out.println("Connected to client "+server.getRemoteSocketAddress());
inputStream = server.getInputStream();
in = new DataInputStream(inputStream);
out = new FileOutputStream("output.txt");
ArrayList<Byte> bytes = new ArrayList<Byte>();
int curi;
byte cur;
byte[] curBytes = null;
int length = 0;
System.out.println("Before while loop");
while((curi = in.read())!=-1 && count!=500)
{
System.out.println(count+" Reading some data");
//out.write(curi);
cur = (byte)curi;
bytes.add(cur);
curBytes = getPrimativeArray(bytes);
String curBytesString = new String(curBytes, "UTF-8");
count++;
}
int i=0;
for(byte b : bytes)
{
System.out.print(b+" ");
curBytes[i] = b;
i++;
}
out.write(curBytes);
server.close();
}
catch(IOException e)
{
e.printStackTrace();
}
What I print using System.out.print(b+" "); and the one I store in curBytes[] are the same thing. But when I compare the console and file output, they are different.
Console output: 0 0 113 -100 -126 -54 0 32 14 1 0 0 1 -58 60 54 0 3 63 -2 85 74 -81 -88 0 9 1 24 85 74 -81 -48 0 13 65 -113 85 74 -81 -88 0 12 125 -126 85 74 -81 -88 0 13 21 97 85 74 -81 -88 0 13 31 19 85 74 -81 -48 0 13 42 24 0 6 0 0 0 0 0 0 0 0 0 0 32 0 7 -100 0 -5 6 -128 0 -56 29 -127 23 112 -1 -1 0 0 64 0 1 -121 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 49 53 64 111 110 101 46 97 116 116 46 110 101 116 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 54 53 64 111 110 101 46 97 116 116 46 110 101 116 37 50 57 54 53 45 49 53 48 53 48 54 50 51 50 55 48 50 45 50 48 53 48 54 54 50 55 54 54 64 48 48 55 56 48 48 49 49 16 32 1 5 6 64 0 0 0 32 16 0 0 0 120 0 17 16 32 1 24 -112 16 1 46 2 0 0 0 0 0 0 0 6 1 -113 0 4 0 33 -64 -42 0 91 5 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -120 -28 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -44 -39 8 0 4 0 -56 0 0 1 11 3 85 74 -81 -88 0 9 1 24 8 0 5 0 0 0 0 0 0 3 85 74 -81 -88 0 13 31 19 8 0 1 0 -56 0 0 0 6 3 85 74 -81 -48 0 13 42 24 -64 34 4 24 9 89 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 98 114 97 110 99 104 61 122 57 104 71 52 98 75 50 57 48 45 48 48 55 56 48 48 49 49 45 48 48 48 102 45 52 52 49 57 55 49 52 48 51 3 85 74 -81 -88 0 12 -120 -28 127 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 114 101 99 101 105 118 101 100 61 50 48 48 49
File Output: ^#^#q<9c><82>Ê^# ^N^A^#^#^AÆ<6^#^C?þUJ¯¨^# ^A^XUJ¯Ð^#^MA<8f>UJ¯¨^#^L}<82>UJ¯¨^#^M^UaUJ¯¨^#^M^_^SUJ¯Ð^#^M*^X^#^F^#^#^#^#^#^#^#^#^#^# ^#^G<9c>^#û^F<80>^#È^]<81>^Wpÿÿ^#^##^#^A<87>^\sip:+14222099915#one.att.net^\sip:+14222099965#one.att.net%2965-150506232702-2050662766#00780011^P ^A^E^F#^#^#^# ^P^#^#^#x^#^Q^P ^A^X<90>^P^A.^B^#^#^#^#^#^#^#^F^A<8f>^#^D^#!ÀÖ^#[^E^H^# ^#È^#^#^#^O^CUJ¯¨^#^L<88>ä^H^# ^#È^#^#^#^O^CUJ¯¨^#^LÔÙ^H^#^D^#È^#^#^A^K^CUJ¯¨^# ^A^X^H^#^E^#^#^#^#^#^#^CUJ¯¨^#^M^_^S^H^#^A^#È^#^#^#^F^CUJ¯Ð^#^M*^XÀ"^D^X YSIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;branch=z9hG4bK290-00780011-000f-441971403^CUJ¯¨^#^L<88>ä^?SIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;received=2001
Please let me know at what step I'm making a mistake.
The other answers here tell you to use a PrintWriter or a FileWriter instead of the FileOutputStream but I'm fairly sure that this is not what you want.
Your problem is that you're writing raw bytes to a file and then reading it back as characters and comparing that to byte values represented as characters and then printed with System.out.
Let's take a look at what happens when you print a byte with the value 65 (or 01000001 in binary).
When you use System.out.print you will invoke PrintStream.print(int) with the integer value of 65 which will in turn print the characters 6 and 5 to the terminal.
When you use out.write you will invoke FileOutputStream.write(byte[]) which will write the bits 01000001 to the file.
Later, when you check the contents of the file your tool will try to interpret this byte as a character and it will most likely use the ASCII encoding to do so (even if you're using Unicode as your default encoding this is likely what will happen since Unicode is a superset of ASCII). This results in the character A being printed.
If you want to view the output file in a way similar to what you've printed with System.out.print you can use the following command on linux:
$ hexdump -e '/1 "%i "' <file>
Example:
$ cat /etc/issue
Ubuntu 12.04.5 LTS \n \l
$ hexdump -e '/1 "%i "' /etc/issue
85 98 117 110 116 117 32 49 50 46 48 52 46 53 32 76 84 83 32 92 110 32
92 108 10 *
My first answer was wrong, so I am editing this because I made the assumption that you could write out a string to the FileOutputStream, but I don't think that is the case. FileOutputStream is only used for byte streams, so you've got to stick to that format when writing out to the file.
If you hold the data in a buffer[array], and then write those bytes out to a file that you have created using the output stream, it should work. I found this document that might be helpful.
The main idea is that somewhere in your code, the byte array isn't getting written to the file correctly. Perhaps its just a matter of adding the close() method.
out.close();
server.close();
reading and writing files in java
Here is the section I found useful.
import java.io.*;
public class Test {
public static void main(String [] args) {
// The name of the file to create.
String fileName = "temp.txt";
try {
// Put some bytes in a buffer so we can
// write them. Usually this would be
// image data or something. Or it might
// be unicode text.
String bytes = "Hello theren";
byte[] buffer = bytes.getBytes();
FileOutputStream outputStream =
new FileOutputStream(fileName);
// write() writes as many bytes from the buffer
// as the length of the buffer. You can also
// use
// write(buffer, offset, length)
// if you want to write a specific number of
// bytes, or only part of the buffer.
outputStream.write(buffer);
// Always close files.
outputStream.close();
System.out.println("Wrote " + buffer.length +
" bytes");
}
catch(IOException ex) {
System.out.println(
"Error writing file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
The console (System.out) is a PrintWriter, while the file output is a FileOutputStream.
The basic difference between Stream and Writer: Streams are supposed to manipulate "raw data", like numbers taken directly from binary format, while writers are used to manipulate "human-readable data", transforming all the data you write.
For example, the 6 int is different from the 6 character. When you use a stream, you write directly the int, while with a writer, the data wrriten is transformed into the character.
Then, if you want your file output to be the same as your console output, do not use FileOutputStream, but instead, use FileWriter, and it's method write(String).
How to make this work:
1 - replace out = new FileOutputStream("output.txt"); by out = new FileWriter("output.txt");
2 - replace out.write(curBytes); by:
for (byte b : curBytes) {
out.write(b + " ");
}
I would suggest you use IOUtils.copy and use a BufferedReader
to wrap your InputStream.
The output stream should obviously be FileOutputStream
I hope this helps

java string with extended ascii codes to byte arrary (allocate one byte per code)

I have a java nio socket server that needs to communicate with a C socket client. The C socket client sends data to the server with ASCII codes above 127, using one byte per ASCII code. The server needs to send back the same.
I need to convert a string into a byte array with the same length (one byte per code).
The string is like 1200üö001001001, where it contains some ascii codes above 127.
In the research I did, I did not find a solution. I tried this
byte[] b = "1200üö001001001".getBytes("UTF-8");
byte[] b = "1200üö001001001".getBytes("US-ASCII");
byte[] b = "1200üö001001001".getBytes("ISO-8859-1");
byte[] b = "1200üö001001001".getBytes();
Some of these convert üö into ?? or multiple bytes per ASCII code, causing a different length between byte array and string.
I needing some help.
This is an example of data that comes from the C socket client. dots are ascii codes from Client.
<code>
0000 00 ff 31 32 30 30 fc f6 00 01 08 e1 e0 00 00 00 ..1200..........
0010 00 00 14 00 00 00 31 36 34 36 30 30 32 38 30 30 ......1646002800
0020 30 30 30 30 30 34 31 38 30 31 32 30 30 30 30 30 0000041801200000
0030 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 0000001000000000
0040 30 30 31 30 30 30 30 30 30 30 30 30 30 30 31 30 0010000000000010
0050 30 30 36 31 30 30 30 30 30 30 36 31 30 30 30 30 0061000000610000
0060 30 30 35 31 35 39 31 31 31 34 30 39 32 33 31 33 0051591114092313
0070 32 31 31 32 33 32 30 35 31 34 30 39 32 33 30 36 2112320514092306
0080 39 39 39 39 39 39 30 30 30 30 30 35 35 31 35 39 9999990000055159
0090 31 31 33 32 39 38 20 20 20 20 33 38 38 33 33 38 113298 388338
00a0 38 33 20 20 20 20 20 20 20 42 41 43 20 50 41 4e 83 XXX PAN
00b0 41 4d 41 20 54 45 53 54 3e 48 4f 57 41 52 44 20 AMA TEST>HOWARD
00c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
00d0 41 30 32 33 30 30 31 30 30 31 31 30 30 32 30 30 A023001001100200
00e0 33 37 30 30 30 32 35 30 30 31 31 38 34 30 38 34 3700025001184084
00f0 30 38 34 30 30 30 30 39 31 31 30 30 30 30 30 30 0840000911000000
0100 31
This is the code i use to convert from hex string to byte array
line 4 i convert from binary eg. 1101 to equvalent hex string eg `3F`
line 13 i take the bitmap in hex string eg. `"fcf6000108e1e0000000000014000000"`
that contains 32 chars and try to convert to 16 bytes, but this results in 20 bytes
because some chars takes more than one byte
1 // Set bitmaps
2 if ( String.copyValueOf(zeros).indexOf("1", 64) == -1 ) { zeros[0] = '0'; }
3 tmp = String.copyValueOf(zeros);
4 bits = binToHex(tmp);
5
6 // check bits to send
7 if ( zeros[0] == '0') {
8 bits = bits.substring(0, 16);
9 }
10
11 // join message
12 tmp = sb.toString();
13 trm = isoCode + hexToASCII(bits) + tmp.substring(39);
14 System.out.println("Trama respuesta " + (new Timestamp((new Date()).getTime())).toString() + " " + trm);
// here the code to send back the message from nio socet to client
line mark with 100 add 2 space because there is a header with a short value that have the length of
the message to be readed by socket client
byte[] bytes = message.getBytes("ISO-8859-1");
writeLength = message.length();
//writeLength = bytes.length;
100 writeBuffer = ByteBuffer.allocate(writeLength + 2);
writeBuffer.putShort( (short)writeLength );
writeBuffer.put(message.getBytes()); // Para test
//this.writeBuffer.putChar('\n');
writeBuffer.flip();
// auxiliar methods
public String hexToASCII(String hex){
if(hex.length()%2 != 0){
System.err.println("requires EVEN number of chars");
return null;
}
StringBuilder sb = new StringBuilder();
//Convert Hex 0232343536AB into two characters stream.
for( int i=0; i < hex.length()-1; i+=2 ){
/*
* Grab the hex in pairs
*/
String output = hex.substring(i, (i + 2));
/*
* Convert Hex to Decimal
*/
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
}
return sb.toString();
}
private final String HEXES = "0123456789ABCDEF";
private final String HEX[] = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
private final String BIN[] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
/*
* Convert binary to hex string
*/
public String binToHex(String bin) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bin.length()-1 ; i += 4) {
sb.append(HEX[ArrayUtils.indexOf(BIN, bin.substring(i, (i + 4)))]);
}
return sb.toString();
}
You are mixing bytes and characters. There are dozens of ways to encode characters like ü and ö into bytes and different encodings take different amount of bytes per character.
DO NOT try to convert binary stream to string unless you know that the bytes represent textual information AND you know which encoding was used to convert the text into bytes in the first place. If the data is not a text string, DO NOT try to interpret it as a text string and just wish that every byte maps to something reasonable.
You can work directly with byte arrays or use e.g. java.nio.ByteBuffer. If there is a text string amongst the data, you can separately convert the peticular bytes into strings.
Your binary bitmap is getting mangled by character encoding conversions. There is no point in doing it that way. You can just convert from hex to byte[].
This code suggests the way:
String bitmapHex = "00112233445566778899aabbccddeeff";
String isoCode = "1200";
String data = "313030303030303031 and more";
byte[] bitmap = javax.xml.bind.DatatypeConverter.parseHexBinary(bitmapHex);
int datagramLength = 2 + isoCode.length() + bitmap.length + data.length();
ByteBuffer buffer = ByteBuffer.allocate(datagramLength);
buffer.order(ByteOrder.BIG_ENDIAN).putShort((short)datagramLength);
buffer.put(isoCode.getBytes(encodingName));
buffer.put(bitmap);
buffer.put(data.getBytes(encodingName));
byte[] output = buffer.array();

Read Java in as Hex

I have tried to solve this but I keep coming up with stuff that is no help I'm sure this is easy (when you know how of course ;) )
What I would like to do is read in a file using a byte stream like below:
while((read = in.read()) != -1){
//code removed to save space
Integer.toHexString(read);
System.out.println(read);
}
When it prints out the Hex to the screen it will print out numbers fine e.g
31
13
12
0
but when it comes to a hex code that should be 01 31 it will print 0 131. I want to read it in to a variable like you would see in a hex editor i.e 00 11 21 31 no single numbers as i need to scan the whole file and look for patterns which I know how to do I'm just stuck on this :/
so in short i need a variabe to contain the two hex characters i.e int temp = 01 not int temp = 0 , I hope this all makes sense, I'm a little confused as it's 3am!
If anyone knows how to do this I would be most greatful, p.s thanks for the help in advance this site has saved me loads of research and have learnt a lot!
Many thanks.
This method :
public static void printHexStream(final InputStream inputStream, final int numberOfColumns) throws IOException{
long streamPtr=0;
while (inputStream.available() > 0) {
final long col = streamPtr++ % numberOfColumns;
System.out.printf("%02x ",inputStream.read());
if (col == (numberOfColumns-1)) {
System.out.printf("\n");
}
}
}
will output something like this :
40 32 38 00 5f 57 69 64 65 43
68 61 72 54 6f 4d 75 6c 74 69
42 79 74 65 40 33 32 00 5f 5f
69 6d 70 5f 5f 44 65 6c 65 74
65 46 69 6c 65 41 40 34 00 5f
53 65 74 46 69 6c 65 50 6f 69
6e 74 65 72 40 31 36 00 5f 5f
69 6d 70 5f 5f 47 65 74 54 65
6d 70 50 61 74 68 41 40 38 00
Is it what you are looking for?
I think what you're looking for is a formatter. Try:
Formatter formatter = new Formatter();
formatter.format("%02x", your_int);
System.out.println(formatter.toString());
Does that do what you're looking for? Your question wasn't all that clear (and I think maybe you deleted too much code from your snippet).
import org.apache.commons.io.IOUtils;
import org.apache.commons.codec.binary.Hex;
InputStream is = new FileInputStream(new File("c:/file.txt"));
String hexString = Hex.encodeHexString(IOUtils.toByteArray(is));
In java 7 you can read byte array directly from file as below :
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path)
Hi everyone one posted, thanks for the reply but the way I eneded up doing it was:
hexIn = in.read();
s = Integer.toHexString(hexIn);
if(s.length() < 2){
s = "0" + Integer.toHexString(hexIn);
}
Just thought I would post they way I did it for anyone else in future, thank you soo much for your help though!

Categories