Reading bytes from a file - java

I am reading from a ".264" file using code below.
public static void main (String[] args) throws IOException
{
BufferedReader br = null;try {
String sCurrentLine;
br = new BufferedReader(new InputStreamReader(new FileInputStream("test.264"),"ISO-8859-1"));
StringBuffer stringBuffer = new StringBuffer();
while ((sCurrentLine = br.readLine()) != null) {
stringBuffer.append(sCurrentLine);
}
String tempdec = new String(asciiToHex(stringBuffer.toString()));
System.out.println(tempdec);
String asciiEquivalent = hexToASCII(tempdec);
BufferedWriter xx = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Users/Administrator/Desktop/yuvplayer-2.3/video dinalized/testret.264"),"ISO-8859-1"));
xx.write(asciiEquivalent);
xx.close();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Opening input and output file in HEX Editor show me some missing values, e.g. 0d (see pictures attached).
Any solution to fix this?

Lose InputStreamReader and BufferedReader, just use FileInputStream on its own.
No character encoding, no line endings, just bytes.
Its Javadoc page is here, that should be all you need.
Tip if you want to read the entire file at once: File.length(), as in
File file = new File("test.264");
byte[] buf = new byte[(int)file.length()];
// Use buf in InputStream.read()

Related

How can I properly read an Arabic dataset in java?

Scenario: I want to read an Arabic dataset with utf-8 encoding. Each word in each line is separated by a space.
Problem: When I read each line, the output is:
??????? ?? ???? ?? ???
Question: How can I read the file and print each line?
for more information, here is my Arabic dataset and part of my source code that reads data would be like the following:
private ContextCountsImpl extractContextCounts(Map<Integer, String> phraseMap) throws IOException {
Reader reader;
reader = new InputStreamReader(new FileInputStream(inputFile), "utf-8");
BufferedReader rdr = new BufferedReader(reader);
while (rdr.ready()) {
String line = rdr.readLine();
System.out.println(line);
List<String> phrases = splitLineInPhrases(line);
//any process on this file
}
}
I can read using UTF-8, Can you try like this.
public class ReadArabic {
public static void main(String[] args) {
try {
String line;
InputStream fileInputStream = new FileInputStream("arabic.txt");
Reader reader = new InputStreamReader(fileInputStream, "UTF-8"); // leave charset out for default
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.err.println(e.getMessage()); // handle all exceptions
}
}
}

How to read a file byte by byte using BufferReader class in java

I want to read my file which is a large one byte by byte and i currently using this class for reading the file:
public class File {
public byte[] readingTheFile() throws IOException {
FileReader in = new FileReader("/Users/user/Desktop/altiy.pdf");
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
in.close();
return null;
}
} //close class
Now in my main class where my main method is i am trying to read the file and then try to pass it as parameter to another method of another class like below:
public class myMainClass {
// some fields here
File f = new File ();
public static void main (String [] a) {
try {
byte[] secret = five.readingTheFile(); // We call the method which read the file
byte[][] shar = one.calculateThresholdScheme(secret, n,k);
// some other code here . Note n and k i put their values from Eclipse
} catch (IOException e) {
e.printStackTrace();
} // close catch
} // close else
} // close main
} // close class
Now in my class where calculateThresholdScheme is
public class performAlgorithm {
// some fields here
protected byte[][] calculateThresholdScheme(byte[] secret, int n, int k) {
if (secret == null)
throw new IllegalArgumentException("null secret");
// a lot of other codes below.
But my execution stops as soon as i throw this IllegalArgumentException("null secret"); which means my file is not yet readable. I am wondering what is going wrong here but i am still not figure it out
The issue with your code lies in readingTheFile():
This is the return-statement:
return null;
Which - Captain Obvious here - returns null. Thus secret is null and the IllegalArgumentException is thrown.
If you absolutely want to stick to the BufferedReader-solution, this should solve the problem:
byte[] readingTheFile(){
byte[] result = null;
try(BufferedReader br = new BufferedReader(new FileReader(path))){
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null)
sb.append(line).append(System.getProperty("line.separator"));
result = sb.toString().getBytes();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
Some general advice:
BufferedReader isn't built for the purpose of reading a file byte for byte anyways. E.g. '\n' will be ignored, since you're reading line-wise. This may cause you to corrupt data while loading. Next problem: you're only closing the FileReader in readingTheFile(), but not the BufferedReader. Always close the ToplevelReader, not the underlying one. By using try-with-resources, you're saving yourself quite some work and the danger of leaving the FileReader open, if something is coded improperly.
If you want to read the bytes from a file, use FileReader instead. That'll allow you to load the entire file as byte[]:
byte[] readingTheFile(){
byte[] result = new byte[new File(path).length()];
try(FileReader fr = new FileReader(path)){
fr.read(result , result.length);
}catch(IOException e){
e.printStackTrace();
}
return result;
}
Or alternatively and even simpler: use java.nio
byte[] readingTheFile(){
try{
return Files.readAllBytes(FileSystem.getDefault().getPath(path));
}catch(IOException e){
e.printStackTrace();
return null;
}
}
Change your Class "File" like below, this is how it will provide you the desire mechanism. I've modified the same class you have written.
public class File {
public byte[] readingTheFile() throws IOException {
java.io.File file = new java.io.File("/Users/user/Desktop/altiy.pdf");
/*FileReader in = new FileReader("/Users/user/Desktop/altiy.pdf");
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
in.close();
*/
FileInputStream fin = null;
try {
// create FileInputStream object
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int) file.length()];
// Reads bytes of data from this input stream into an array of
// bytes.
fin.read(fileContent);
// returning the file content in form of byte array
return fileContent;
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading file " + ioe);
} finally {
// close the streams using close method
try {
if (fin != null) {
fin.close();
}
} catch (IOException ioe) {
System.out.println("Error while closing stream: " + ioe);
}
}
return null;
}
}

Reading a variable number of lines from a file

I am trying to read a variable number of lines from a file, hopefully using InputStream object. What I'm trying to do (in a very general sense) is as follows:
Pass in long maxLines to function
Open InputStream and OutputStream for reading/writing
WHILE (not at the end of read file AND linesWritten < maxLines)
write to file
I know InputStream goes on bytes, not lines, so I'm not sure if that's a good API to use for this. If anyone has any reccomendations on what to look at in terms of a solution (other API's, different algorithm) that's be very helpful.
You can have something like this
BufferedReader br = new BufferedReader(new FileReader("FILE_LOCATION"));
while (br.readLine() != null && linesWritten < maxLines) {
//Your logic goes here
}
Have a look at these:
Buffered Reader and
Buffered Writer
//Read file into String allText
InputSream fis = new FileInputStream("filein.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line, allText = "";
try {
while ((line = br.readLine()) != null) {
allText += (line + System.getProperty("line.separator")); //Track where new lines should be for output
}
} catch(IOException e) {} //Catch any errors
br.close(); //Close reader
//Write allText to new file
BufferedWriter bw = new BufferedWriter(new FileWriter("fileout.txt"));
try {
bw.write(allText);
} catch(IOException e) {} //Catch any errors
bw.close(); //Close writer

Modify the content of a file using Java

I want to delete some content of file using java program as below. Is this the write method to replace in the same file or it should be copied to the another file.
But its deleting the all content of the file.
class FileReplace
{
ArrayList<String> lines = new ArrayList<String>();
String line = null;
public void doIt()
{
try
{
File f1 = new File("d:/new folder/t1.htm");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
while (line = br.readLine() != null)
{
if (line.contains("java"))
line = line.replace("java", " ");
lines.add(line);
}
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
out.write(lines.toString());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public statc void main(String args[])
{
FileReplace fr = new FileReplace();
fr.doIt();
}
}
I would start with closing reader, and flushing writer:
public class FileReplace {
List<String> lines = new ArrayList<String>();
String line = null;
public void doIt() {
try {
File f1 = new File("d:/new folder/t1.htm");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
if (line.contains("java"))
line = line.replace("java", " ");
lines.add(line);
}
fr.close();
br.close();
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
for(String s : lines)
out.write(s);
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String args[]) {
FileReplace fr = new FileReplace();
fr.doIt();
}
}
The accepted answer is great. However, there is an easier way to replace content in a file using Apache's commons-io library (commons-io-2.4.jar - you can use any latest versions)
private void update() throws IOException{
File file = new File("myPath/myFile.txt");
String fileContext = FileUtils.readFileToString(file);
fileContext = fileContext.replaceAll("_PLACEHOLDER_", "VALUE-TO-BE-REPLACED");
FileUtils.write(file, fileContext);
}
Note: Thrown IOException needs to be caught and handled by the application accordingly.
Read + write to the same file simulatenously is not ok.
EDIT: to rephrase and be more correct and specific - reading and writing to the same file, in the same thread, without properly closing the reader (and flusing the writer) is not ok.
Make sure to:
close any stream when you no longer need them
In particular before reopening it for writing.
truncate the file, to make sure it shrinks if you write less than it had.
then write the output
write individual lines, don't rely on toString.
flush and close when you are finished writing!
If you use buffered IO, you always have to ensure that the buffer is flushed at the end, or you might lose data!
I can see three problems.
First you are writing to out which I assume is System.out, not an output stream to the file.
Second, if you do write to an output stream to the file, you need to close it.
Third, the toString() method on an ArrayList isn't going to write the file like you are expecting. Loop over the list and write each String one at a time. Ask yourself whether you need to write newline characters as well.
The accepted answer is slightly wrong. Here's the correct code.
public class FileReplace {
List<String> lines = new ArrayList<String>();
String line = null;
public void doIt() {
try {
File f1 = new File("d:/new folder/t1.htm");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
if (line.contains("java"))
line = line.replace("java", " ");
lines.add(line);
}
fr.close();
br.close();
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
for(String s : lines)
out.write(s);
out.flush();
}
out.close();
catch (Exception ex) {
ex.printStackTrace();
}
}

Return the text of a file as a string? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to create a Java String from the contents of a file
Is it possible to process a multi-lined text file and return its contents as a string?
If this is possible, please show me how.
If you need more information, I'm playing around with I/O. I want to open a text file, process its contents, return that as a String and set the contents of a textarea to that string.
Kind of like a text editor.
Use apache-commons FileUtils's readFileToString
Check the java tutorial here -
http://download.oracle.com/javase/tutorial/essential/io/file.html
Path file = ...;
InputStream in = null;
StringBuffer cBuf = new StringBuffer();
try {
in = file.newInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
cBuf.append("\n");
cBuf.append(line);
}
} catch (IOException x) {
System.err.println(x);
} finally {
if (in != null) in.close();
}
// cBuf.toString() will contain the entire file contents
return cBuf.toString();
Something along the lines of
String result = "";
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
// Here's where you get the lines from your file
result += dis.readLine() + "\n";
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
String data = "";
try {
BufferedReader in = new BufferedReader(new FileReader(new File("some_file.txt")));
StringBuilder string = new StringBuilder();
for (String line = ""; line = in.readLine(); line != null)
string.append(line).append("\n");
in.close();
data = line.toString();
}
catch (IOException ioe) {
System.err.println("Oops: " + ioe.getMessage());
}
Just remember to import java.io.* first.
This will replace all newlines in the file with \n, because I don't think there is any way to get the separator used in the file.

Categories

Resources