How to read url byte by byte? - java

Hi i am trying to read a url where i am getting a string i am printing that string on console but i want to read that url as a byte by byte which i am not getting how can i read
Here is my ReadTextFromURL
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadTextFromURL {
public static void main(String[] args) {
try {
URL url = new URL("http://122.160.81.37:8080/mandim/MarketWise?m=agra");
ByteArrayOutputStream bais = new ByteArrayOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
int lin;
while ((lin = in.read()) != -1) {
System.out.println(lin);
}
in.close();
} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
}
desired output
धान~1325|चावल~2050|ज्वर~920|जौ~810|मकई~1280|गेहूँ~1420|जो~1050|बेजर~-|जय~800
getting output
2343
2366
2344
126
49
51
50
53
124
2330
2366
2357
2354
126
50
How can I get my desired output?

You can open the URL as InputStream and use the byte oriented method specifying an array of size equal to 1. Have a look to this page: http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[],%20int,%20int)
By the way, use the try-with-resource construct construct when working with streams:
byte oneSizeByteArray = new byte[1];
try (InputStream is = url.openStream()) {
is.read(oneSizeByteArray,0,1)
} catch (IOException ex) {
}

If you want to read bytes, don't use Readers. A Reader reads chars (not bytes). If you need to read bytes, use low-level input-output classes (InputStream/OutputStream).

Related

How do I use an inputStream as the text to an outputStream? [duplicate]

This question already has answers here:
Standard concise way to copy a file in Java?
(16 answers)
copy file with stream
(3 answers)
Best way to Pipe InputStream to OutputStream [duplicate]
(4 answers)
Closed 5 years ago.
I have two files, GettysburgAddress.txt, where the Gettysburg Address is written, and GettysburgAddressCopy.txt, which is an empty text file that I'm supposed to fill in with the Gettysburg Address, each sentence, till its period, on a different line.
So I thought of this
import java.io.PrintWriter;`
import java.io.FileNotFoundException;v`
import java.util.Scanner;`
import java.io.File;
public class UltimateTextFileOutputDemo
{
public static void main(String[] args)
{
String writtenFileName = "C:\\Documents and Settings\\GettysburgAddressCopy.txt";
PrintWriter outputStream = null;
try
{
outputStream = new PrintWriter(writtenFileName);
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file" + writtenFileName);
System.exit(0);
}
String readFileName = "C:\\Documents and Settings\\GettysburgAddress.txt";
Scanner inputStream = null;
try
{
inputStream = new Scanner(new File(readFileName));
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file " +
readFileName);
System.exit(0);
}
while (inputStream.hasNextLine())
{
inputStream.useDelimiter("."); // setting the period as the delimiter of the read piece of text, I'm sure it gets a single, complete sentence
String line = inputStream.nextLine();
outputStream.println(line);
}
outputStream.close();
inputStream.close();
System.out.println("The Gettysburg Address was written to " + writtenFileName);
}
}
When run, the program rightly creates the GettysburgAddressCopy.txt, if it doesn't exist yet, but it doesn't fill it with the speech. I realize the code naïvety is all in the three lines
inputStream.useDelimiter(".");
String line = inputStream.nextLine();
outputStream.println(line);
but then, what's the right code to write?
Many thanks for giving me the best tips you can.
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class UltimateTextFileOutputDemo
{
public static void main(String[] args)
{
String writtenFileName = "D:\\testdump\\GettysburgAddressCopy.txt";
PrintWriter outputStream = null;
try
{
outputStream = new PrintWriter(writtenFileName);
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file" + writtenFileName);
System.exit(0);
}
String readFileName = "D:\\testdump\\GettysburgAddress.txt";
Scanner inputStream = null;
try
{
inputStream = new Scanner(new File(readFileName));
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file " +
readFileName);
System.exit(0);
}
inputStream.useDelimiter("\\.");
while (inputStream.hasNextLine())
{
// setting the period as the delimiter of the read piece of text, I'm sure it gets a single, complete sentence
String line = inputStream.next();
outputStream.println(line);
}
outputStream.close();
inputStream.close();
System.out.println("The Gettysburg Address was written to " + writtenFileName);
}
}

Can't figure out why index is out of bound for IO file

This program reads from card.raw and creates a jpg. I could create the first image sucessfully, but I can't seem to figure out why i get an index out of bound error for the second image
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class Recoverytst {
public static void main (String[] args) throws IOException {
try {
FileInputStream fs = new FileInputStream("card.raw");
FileOutputStream os = new FileOutputStream("1.jpg");
byte[] fileContent = new byte[512];
while (fs.read(fileContent) != -1) {
os.write(fileContent);
}
fs.close();
os.close();
}
catch(IOException ioe) {
System.out.println("Error " + ioe.getMessage());
}
try {
FileInputStream fs2 = new FileInputStream("card.raw");
FileOutputStream os2 = new FileOutputStream("2.jpg");
byte[] fileContent2 = new byte[512];
while (fs2.read(fileContent2) != -1) {
Cant figure out why i get index out of bound error over here for the line below
os2.write(fileContent2,513,512);
}
fs2.close();
os2.close();
}
catch(IOException ioe) {
System.out.println("Error " + ioe.getMessage());
}
}
}
You wrote
os2.write(fileContent2,513,512);
What this means is every time it executes, you are trying to write 512 bytes from the array skipping 513 bytes but the array is only 512 bytes long. So it won't fit.
Try this..
File file = new File("path to card.raw");
long len = file.length();
byte[] fileContent = new byte[len];
fs2.read(fileContent);
After that use
os2.write(fileContent2,513,512);
out side the loop only once. It will write 512 bytes of data starting from the 513 byte.
This is normal in the way that you chose an arbitrary size of the byte array (512) and the image file size must be bigger than 512.

use Java to convert ANY file to hex and back again

I have some files i would like to convert to hex, alter, and then reverse again, but i have a problem trying to do jars, zips, and rars. It seems to only work on files containing normally readable text. I have looked all around but cant find anything that would allow jars or bats to do this correctly. Does anyone have an answer that does both? converts to hex then back again, not just to hex?
You can convert any file to hex. It's just a matter of obtaining a byte stream, and mapping every byte to two hexadecimal numbers.
Here's a utility class that lets you convert from a binary stream to a hex stream and back:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
public class Hex {
public static void binaryToHex(InputStream is, OutputStream os) {
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
try {
int value;
while ((value = is.read()) != -1) {
writer.write(String.format("%02X", value));
}
writer.flush();
} catch (IOException e) {
System.err.println("An error occurred");
}
}
public static void hexToBinary(InputStream is, OutputStream os) {
Reader reader = new BufferedReader(new InputStreamReader(is));
try {
char buffer[] = new char[2];
while (reader.read(buffer) != -1) {
os.write((Character.digit(buffer[0], 16) << 4)
+ Character.digit(buffer[1], 16));
}
} catch (IOException e) {
System.err.println("An error occurred");
}
}
}
Partly inspired by this sample from Mykong and this answer.
Don't use a Reader to read String / char / char[], use an InputStream to read byte / byte[].

java application that converts CSV to Json

I'm trying to create a Java application to convert Excel/csv file content to JSON format,
As all my output json files have the same header. I chose to simplify by using a classic method with BufferedReader and BufferedWriter. Here is a portion of my code:
BufferedReader csvFile= new BufferedReader(new FileReader("DataTextCsv.csv"));
BufferedWriter jsonFile=new BufferedWriter(new FileWriter("converted.txt"));
String fileContent = csvFile.readLine();
// set the constant header
String jsonScript="constant header of json content";
while (fileContent !=null){
fileContent=csvFile.readLine();
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript+="\""+tab[0]+"\" :";
jsonScript+=tab[1]+","+"\n";
// End of json content construction
jsonScript=jsonScript.substring(0,jsonScript.length()-2);
jsonScript+="}";
String[] tabWrite=jsonScript.split("\n");
for (String item:tabWrite){
jsonFile.write(item);
jsonFile.newLine();
}
csvFile.close();
jsonFile.close();
}
The application can correctly read the first line of the csv file but can not continue till the end and I continuously get this error (even if I try to set all my csv data in one line:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at CSVConverter.main(CSVConverter.java:17)
I'm conscious that it would be simpler to use more specific libraries, but as I'm new with Java, I wasn't able to find the right package to download and install
Move the close statements out of the while loop (preferably into a finally block)
csvFile.close();
jsonFile.close();
See the following, you had your close methods in the wrong location.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewClass {
public static void main(String[] args) {
BufferedReader csvFile = null;
BufferedWriter jsonFile = null;
try {
csvFile = new BufferedReader(new FileReader("DataTextCsv.csv"));
jsonFile = new BufferedWriter(new FileWriter("converted.txt"));
String fileContent = csvFile.readLine();
// set the constant header
String jsonScript = "constant header of json content";
while (fileContent != null) {
fileContent = csvFile.readLine();
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript += "\"" + tab[0] + "\" :";
jsonScript += tab[1] + "," + "\n";
// End of json content construction
jsonScript = jsonScript.substring(0, jsonScript.length() - 2);
jsonScript += "}";
String[] tabWrite = jsonScript.split("\n");
for (String item : tabWrite) {
jsonFile.write(item);
jsonFile.newLine();
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
csvFile.close();
jsonFile.close();
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Or option two using try-with resource
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewClass {
public static void main(String[] args) {
try (BufferedReader csvFile = new BufferedReader(new FileReader("DataTextCsv.csv")); BufferedWriter jsonFile = new BufferedWriter(new FileWriter("converted.txt"))) {
String fileContent = csvFile.readLine();
// set the constant header
String jsonScript = "constant header of json content";
while (fileContent != null) {
fileContent = csvFile.readLine();
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript += "\"" + tab[0] + "\" :";
jsonScript += tab[1] + "," + "\n";
// End of json content construction
jsonScript = jsonScript.substring(0, jsonScript.length() - 2);
jsonScript += "}";
String[] tabWrite = jsonScript.split("\n");
for (String item : tabWrite) {
jsonFile.write(item);
jsonFile.newLine();
}
}
csvFile.close();
jsonFile.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
You need to add another catch for a null value in the while loop. Also I'm not sure if you intended to have all the lines repeated as the are now. This way returns only the last line/full set of data
while (fileContent !=null){
fileContent=csvFile.readLine();
if (fileContent != null){
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript+="\""+tab[0]+"\" :";
jsonScript+=tab[1]+","+"\n";
// End of json content construction
jsonScript=jsonScript.substring(0,jsonScript.length()-2);
jsonScript+="}";
}else{
String[] tabWrite=jsonScript.split("\n");
for (String item:tabWrite){
result.append(item);
jsonFile.write(item);
jsonFile.newLine();
}
}
}
csvFile.close();
jsonFile.close();

Writing to and reading from file - Java - Not reading what I wrote into file

I'm executing an encryption algorithm and I need your help regarding writing to and reading from a .xtt file in Java. As part of the encryption, I basically need to write Base64 encoded bytes into a .txt file and read these exact bytes, decode them and use them to execute the decryption process.
I seem to be reading something different compared to what I'm writing into the .txt file. Basically when I check the bytearray I'm writing into the file it is reads as [B#56e5b723 but when I read it of my file it produces [B#35a8767.
Here's the outcome as printed in my Java console:
***Numbs converted to ByteArray is as follows: [B#56e5b723
Size of NumbsByteArray is: 10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS STARTED -x-x-x-x-x
6,7,8,9,10 has been received as a byte array in WriteByteArrayToFile(byte[] encoded): [B#56e5b723
6,7,8,9,10 IS TO BE WRITTEN TO THE FILE: /Users/anmonari/Desktop/textfiletwo.txt
bs.write(encoded); HAS BEEN CALLED
-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS ENDED -x-x-x-x-x
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-x-x-x-x-x ReadByteArray() HAS STARTED -x-x-x-x-x
fileData read as bytes is: [B#35a8767
Size of fileData is: 10
fileDataString when converted to a string using String object is����������
fileDataString when converted to a string using fileDataStringTwo.toString()[B#35a8767
fileDataString.getBytes(); is: [B#2c6f7ce9
-x-x-x-x-x ReadByteArray() HAS ENDED -x-x-x-x-x***
Below is my code:
package com.writeandreadfromfile;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteAndRead {
public static void main(String j[]) {
String Numbs = "6,7,8,9,10";
byte[] NumbsByteArray = Numbs.getBytes();
System.out.println("Numbs converted to ByteArray is as follows: " + NumbsByteArray);
System.out.println("Size of NumbsByteArray is: " + NumbsByteArray.length);
System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
WriteByteArrayToFile(NumbsByteArray);
System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
try {
ReadByteArrayFromFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Write ByteArray To File
public static void WriteByteArrayToFile(byte[] NumbsByteArray) {
System.out.println("\n-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS STARTED -x-x-x-x-x");
System.out.println("6,7,8,9,10 has been received as a byte array in WriteByteArrayToFile(byte[] encoded): " + NumbsByteArray);
String fileName = "/Users/anmonari/Desktop/textfiletwo.txt";
System.out.println("6,7,8,9,10 IS TO BE WRITTEN TO THE FILE: " + fileName);
BufferedOutputStream bs = null;
try {
FileOutputStream fs = new FileOutputStream(new File(fileName));
bs = new BufferedOutputStream(fs);
bs.write(NumbsByteArray);
System.out.println("bs.write(encoded); HAS BEEN CALLED");
bs.close();
bs = null;
} catch (Exception e) {
e.printStackTrace();
}
if (bs != null) try { bs.close(); } catch (Exception e) {}
System.out.println("-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS ENDED -x-x-x-x-x");
}
// Read ByteArray To File
public static void ReadByteArrayFromFile() throws IOException {
// Create FileInputStream and feed it the file name
System.out.println("-x-x-x-x-x ReadByteArray() HAS STARTED -x-x-x-x-x");
File file;
try {
file = new File("/Users/anmonari/Desktop/textfiletwo.txt");
// Create the object of DataInputStream
DataInputStream in = new DataInputStream((new FileInputStream(file)));
byte[] fileData = new byte[(int)file.length()];
System.out.println("fileData read as bytes is from file: " + fileData);
System.out.println("Size of fileData is: " + fileData.length);
//String fileDataString = in.readLine();
String fileDataString = new String(fileData);
System.out.println("fileDataString when converted to a string using String object is" + fileDataString);
String fileDataStringTwo = fileData.toString();
System.out.println("fileDataString when converted to a string using fileDataStringTwo.toString()" + fileDataStringTwo);
fileDataString.getBytes();
System.out.println("fileDataString.getBytes(); is: " + fileDataString.getBytes());
//Close the input stream
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-x-x-x-x-x ReadByteArray() HAS ENDED -x-x-x-x-x");
}
}
Any assistance regarding how to read from a file the exact byte array you wrote onto a file is appreciated!
You're not printing the contents of the byte arrays. You're outputting their type and hashCode (the result of the toString() method on arrays).
To output the contents of a byte array, use
System.out.println(java.util.Arrays.toString(array));
In addition to what Sotirios pointed out (you're not reading from in), you're writing the byte array as text, i.e. "6,7,8,9,10", but then you're calling fileDataString.getBytes();. That gets the bytes of the string, which (assuming UTF-8) will be 0x36 0x2c 0x37 0x2c 0x38 0x2c 0x39 0x2c 0x31 0x30.
If your plan is that the file will be text, you need to parse the string back into a byte array. Maybe something like
String[] numbers = fileDataString.split();
byte[] bytes = new byte[numbers.length];
for (int i = 0; i < numbers.length; i++) {
bytes[i] = Byte.parseByte(numbers[i]);
}
On the other hand, if you just need to save and restore byte arrays, you'll have a simpler time with just an ObjectOutputStream and ObjectInputStream.
You can try bs(fs).flush() which buffered everything which were written out

Categories