I am trying to read a text file from a specified path but have this error The System cannot find the file specified
This is the code:
File file=new File("D:\\Progs\\FinalCS\\Records\\record.txt");
FileReader FileR=new FileReader(file);
Scanner scan=new Scanner(FileR);
String uri = "d:\\123.txt"
FileInputStream fis = new FileInputStream(uri);
InputStreamReader isr =new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String s;
while((s=br.readLine())!=null){
// read line
}
Related
I'm currently working with BufferedReader, and was wondering, When we create a new BufferedReader with an InputStreamReader (i.e new BufferedReader(new InputStreamReader(FileInputStream)) as the parameter for it, does that mean the BufferedReader is already reading in the entire bytes of a file from the FileInputStream via InputStreamReader? Or is it only reading the necessary bytes as it's controlled to (i.e readLine()).
Below is an example of my code:
File inputFile = new File("C:\\foo\\foo.txt");
FileInputStream fis = new FileInputStream(inputFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = reader.readLine();
From Input Stream i am reading the image data and convert it to string. From string am writing to an image directly by following type.
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
final char[] cbuf = new char[1024];
final int length = reader.read(cbuf);
String packet=new String(cbuf,0,length);
BufferedWriter out = null ;
FileWriter fstream ;
File file = new File(fileName);
fstream = new FileWriter(file);
out.write(packet);
Please guide me in this issue.
I am not getting full image.
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Decodes input using default encoding potentially corrupting data.
out.write(packet);
Encodes characters using default encoding potentially corrupting data.
Read documentation on API you use. Only perform conversion with default or unknown encoding when you absolutely need it.
Read/convert an InputStream to a String
Please help me out here... the file name is being sent, the contents of the file is also read by the client, but the contents could not be sent to the server and vice versa...
Here is the server side code...
import java.io.*;
import java.net.*;
public class Server {
public static void main(String [] args) throws Exception{
ServerSocket s_sock = new ServerSocket(4567);
System.out.println("Server ready for connection...");
Socket c_sock = s_sock.accept();
System.out.println("Connection established ...server listening...");
// reading the filename from client
InputStream is1 = c_sock.getInputStream();
BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
String f_name1 = br1.readLine();
System.out.println("File sent from client is :" + f_name1);
// receiving file contents from client and copying
String contents1 = null;
InputStream is2 = c_sock.getInputStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
FileWriter fw1 = new FileWriter("from_Client.txt");
BufferedWriter bw1 = new BufferedWriter(fw1);
while((contents1= br2.readLine()) != null) {
bw1.write(contents1);
}
System.out.println("File copied successfully");
// reading the filename from client and sending contents to client
InputStream is3 = c_sock.getInputStream();
BufferedReader br3 = new BufferedReader(new InputStreamReader(is3));
String f_name2 = br3.readLine();
System.out.println("File required by client is :" + f_name2);
String contents2 = null;
FileReader fr2 = new FileReader(f_name2);
BufferedReader br4 = new BufferedReader(fr2);
OutputStream os1 = c_sock.getOutputStream();
PrintWriter pw1 = new PrintWriter(os1, true);
while((contents2 = br4.readLine()) != null) {
pw1.println(contents2);
}
}
}
and here is the client side code
import java.net.*;
import java.io.*;
public class Client {
public static void main(String args[]) throws Exception {
Socket c_sock = new Socket("127.0.0.1", 4567);
//specify file name to send to server
System.out.print("Enter the file name to upload to the server:"+"\t");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String f_name1 = br1.readLine();
OutputStream os1 = c_sock.getOutputStream();
PrintWriter pw1 = new PrintWriter(os1, true);
pw1.println(f_name1);
//sending file contents to server
String contents1 = null;
FileReader fr1 = new FileReader(f_name1);
BufferedReader br2 = new BufferedReader(fr1);
OutputStream os2 = c_sock.getOutputStream();
PrintWriter pw2 = new PrintWriter(os2, true);
while((contents1 = br2.readLine()) != null) {
pw2.println(contents1);
}
//specify file name to send to server
System.out.println();
System.out.print("Now enter the file name to download from server:"+"\t");
BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
String f_name2 = br3.readLine();
OutputStream os3 = c_sock.getOutputStream();
PrintWriter pw3 = new PrintWriter(os3, true);
pw3.println(f_name2);
// receiving file contents from server and copying
String contents2 = null;
InputStream is1 = c_sock.getInputStream();
BufferedReader br4 = new BufferedReader(new InputStreamReader(is1));
FileWriter fw1 = new FileWriter("from_Server.txt");
BufferedWriter bw1 = new BufferedWriter(fw1);
while((contents2 = br4.readLine()) != null) {
bw1.write(contents2);
}
System.out.println("File copied successfully");
}
}
Get rid of the second BufferedReader and use the first for everything. They are stealing data from each other.
Same applies at the client, and also to the two PrintWriters.
Of course the entire approach will only work for text files. If you send binary files through this code, they will be corrupted by the char conversions.
I have a variable, inFileName of type JFileChooser.
I've called this variable to the method HexFinder in class checksumFinder. It is to be used in the inputStreamReader inside a BufferedReader. (I'm using this line to call it)
cf.HexFinder(inFileName,null,null,null);
This is causing an error as the inputStreamReader will only accept variables of type String. (Here's my code for the BufferedReader)
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(inFileName)));
Is there a way that I can get the inputStreamReader to read in inFileName? If not then how can I resolve this?
Any help is much appreciated.
If you are trying to read a file chosen by a JFileChooser then you can do the following;
File file = inFileName.getSelectedFile();
BufferedReader reader = new BufferedReader(new FileReader(file));
Note that FileReader uses the default character encoding. You can manually specify the encoding like this;
String charset = "UTF-8";
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
I have initialized an InputStream in a single method in a class and passing it to next method for processing. The InputStream essentially encapsulates CSV file for processing.
Another method calls 2 different methods passing in same InputStream one for retrieving headers and another for processing contents. The structure looks something as given below:
main() {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
processCSV(isr);
}
processCSV(Reader isr) {
fetchHeaders(isr);
processContentRows(isr);
}
fetchHeaders(Reader isr) {
//Use BufferedReader to retrieve first line of CSV
//Even tried mark() and reset() here
}
processContentRows(Reader isr) {
//Cannot read the values, fetches null from InputStream :(
}
Am I doing something wrong here? Is there any way I can reuse InputStream across different method calls.
I am putting up complete program that can mimic the issue below:
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class MarkResetTest
{
public static void main(String a[])
{
FileInputStream fis = null;
BufferedInputStream bis = null;
InputStreamReader isr = null;
BufferedReader br = null;
BufferedReader br2 = null;
try {
fis = new FileInputStream("C:/Test/Customers.csv");
bis = new BufferedInputStream(fis);
isr = new InputStreamReader(bis, "Unicode");
System.out.println("BR readLine()");
br = new BufferedReader(isr);
//System.out.println(br.markSupported());
br.mark(1000);
System.out.println(br.readLine());
br.reset();
//System.out.println(br.readLine());
System.out.println("BR2 readLine()");
br2 = new BufferedReader(isr);
System.out.println(br2.readLine());
}
catch(Exception e) {
System.out.println("Exception::" + e);
}
finally {
try {
br.close();
isr.close();
bis.close();
fis.close();
}
catch(Exception e) {
System.out.println("Exception while closing streams :: " + e);
}
}
}
}
The problem is in creating two BufferedReaders on top of the same Reader. When you read data from BufferedReader, it's likely to read more than the data it returns, into its buffer (hence the name). In other words, even though you've only read a single line from the BufferedReader, the InputStreamReader may have had a lot more data read from it - so if you read again from that InputStreamReader then you'll miss that data. The data has effectively been sucked from the InputStreamReader to the BufferedReader, so the only way of getting it out to client code is to read it from that BufferedReader.
In other words, your claim that:
Nope. fetchHeaders() only reads first line of CSV containing Headers.
is incorrect. It only uses that much data, but it reads more from the InputStreamReader.
As Ilya said, you should only create one BufferedReader on top of the original InputStreamReader, and pass that into both methods.
fetchHeaders can then use that BufferedReader to read a line, and processContentRows can do what it likes with the BufferedReader at that point - it's just a Reader as far as it needs to know.
So to modify Ilya's example slightly:
public static void main(String[] args) {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr);
processCSV(br);
}
private static void processCSV(BufferedReader reader) {
fetchHeaders(reader);
processContentRows(reader);
}
private static void fetchHeaders(BufferedReader reader) {
// Use reader.readLine() here directly... do *not* create
// another BufferedReader on top.
}
private static void processContentRows(Reader reader) {
// This could be declared to take a BufferedReader if you like,
// but it doesn't matter much.
}
You're not doing anything wrong. Just make sure that the method opening a stream/reader also closes it, in a finally block.
If you need a BufferedReader, I think you need to create it in in main method:
main() {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr);
processCSV(br);
}
processCSV(Reader isr) {
fetchHeaders(isr);
processContentRows(isr);
}
fetchHeaders(Reader isr) {
//Use BufferedReader to retrieve first line of CSV
//Even tried mark() and reset() here
}
processContentRows(Reader isr) {
//Cannot read the values, fetches null from InputStream :(
}
You can do this by using byte
public static void main(String[] args) throws IOException {
String str = "01 Lorem ipsum\n 02 dolor sit\n 03 amet\n";
InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) > -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
InputStream in1 = new ByteArrayInputStream(outputStream.toByteArray());
InputStream in2 = new ByteArrayInputStream(outputStream.toByteArray());
InputStreamReader isr1 = new InputStreamReader(in1);
BufferedReader br1 = new BufferedReader(isr1);
InputStreamReader isr2 = new InputStreamReader(in2);
BufferedReader br2 = new BufferedReader(isr2);
System.out.println("One line from br1:");
System.out.println(br1.readLine());
System.out.println();
System.out.println("One line from br2:");
System.out.println(br2.readLine());
System.out.println(br2.readLine());
System.out.println();
}