Stream writer on char[] does not work - java

I have the following code, but the char[] cc does not seem to get written when I checked the output file. Can someone tell me what's wrong?
import java.io.*;
class Test {
public static void main(String[] args) {
System.out.printf("start of main\n");
char[] cc = new char[300];
try {
String s = "this is a test.";
System.arraycopy(s.toCharArray(), 0, cc, 0, s.length());
System.out.printf("cc = %s\n", new String(cc));
String filename = "tst.data";
DataOutputStream ostream = new DataOutputStream(new FileOutputStream(filename));
OutputStreamWriter writer = new OutputStreamWriter(ostream);
writer.write(cc, 0, 300);
ostream.close();
DataInputStream istream = new DataInputStream(new FileInputStream(filename));
InputStreamReader reader = new InputStreamReader(istream);
char[] newcc = new char[300];
reader.read(newcc, 0, 300);
istream.close();
System.out.printf("newcc = %s\n", new String(newcc));
} catch (Exception e) {
System.out.printf("Exception - %s\n", e);
}
}
}

You need to close the outermost I/O wrapper.
Replace
ostream.close();
by
writer.close();
Unrelated to the concrete problem, those DataOutputStream and DataInputStream wrappers are unnecessary in this context. Remove them. Finally, you should be closing the streams in a finally block. See also this related question: Do I have to close FileOutputStream which is wrapped by PrintStream?

It seams that writer still not flush buffer to ostream.
You can use writer.flush();
like this
writer.write(cc, 0, 300);
writer.flush();

Related

image not showing when transferred using InputStream and OutputStream in java

This is my test program. I need it to apply somewhere.This may be small, sorry for that. But I'm a starter still. So kindly help me.
try{
File file1 = new File("c:\\Users\\prasad\\Desktop\\bugatti.jpg");
File file2 = new File("c:\\Users\\prasad\\Desktop\\hello.jpg");
file2.createNewFile();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
String data = null;
StringBuilder imageBuild = new StringBuilder();
while((data = reader.readLine())!=null){
imageBuild.append(data);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new PrintWriter(new FileOutputStream(file2)));
writer.write(imageBuild.toString());
writer.close();
}catch(IOException e){
e.printStackTrace();
}
This is file1
and This is file2
You can do either of these two:
private static void copyFile(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
or maybe this if you want to use streams:
private static void copyFile(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}
Images do not contain lines or even characters. You therefore should not be using readLine() or even Readers or Writers. You should rewrite the copy loop using input and output streams directly.

I can't write two times over the same file (java)

I am writing to a text file, the first time the a FileOutputStream, and the second time through a FileWriter.
The FileOutputStream seems to be writing to the text file just fine. However, the FileWriter does not. After the FileWriter executes, the content of the file disappears, however it does not get replaced by what the FileWriter is supposed to write. Below is my code:
try {
serverSocket = new ServerSocket(850);
if(!serverSocket.isBound())
System.out.println("Sever Socket not Bounded...");
else
System.out.println("Server Socket bounded to Port : "+serverSocket.getLocalPort());
clientSocket = serverSocket.accept();
if(!clientSocket.isConnected())
System.out.println("Client Socket not Connected...");
else
System.out.println("Client Socket Connected : "+clientSocket.getInetAddress());
InputStream in = clientSocket.getInputStream();
int byteRead = 0;
byte[] byteArray = new byte[1024];
//Write the action in a file
actionFile = new File("C:\\Users\\Khoury\\Documents\\MATLAB\\FYP.txt");
FileOutputStream fos = new FileOutputStream(actionFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while((byteRead = in.read(byteArray, 0, byteArray.length))> 0){
bos.write(byteArray, 0, byteRead);
}
serverSocket.close();
clientSocket.close();
in.close();
bos.close();
fos.close();
Scanner scan = new Scanner(actionFile);
actionChar = scan.next().charAt(0);
if(actionChar == '1' || actionChar == '2'){
FileWriter fw = new FileWriter(actionFile);
BufferedWriter bw2 = new BufferedWriter(fw);
PrintWriter pw2 = new PrintWriter(bw2);
pw2.write('r');
}
System.out.println("Action here: " + actionChar);
}catch (IOException e) {
e.printStackTrace();
}
You have a Scanner and a FileWriteropen to the same pathname at the same time, which is "unhealthy".
Read and close, then create the Writer.
And make sure to call close() after writing! This is missing.

Android App Internal Data Storage

I am attempting to save an array of longs as a text file using the Android's internal memory, and then access that array in a different activity. After noticing that was broken, I isolated the problem in this chunk of code below.
long[] timings = {200, 400, 600, 1400};
try {
FileOutputStream fos = openFileOutput("timings", Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(Arrays.toString(timings));
osw.close();
FileInputStream fis = openFileInput("timings");
InputStreamReader isReader = new InputStreamReader(fis);
char[] buffer = new char[timings.length];
isReader.read(buffer);
textField.setText(Arrays.toString(buffer));
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
The text field that is set to display what is read from the file has the following result:
[[,2,0,0]
The result I expected was the original array from above. What am I doing wrong here? Any help or advice would be appreciated.
Replace this :
InputStreamReader isReader = new InputStreamReader(fis);
char[] buffer = new char[timings.length];
isReader.read(buffer);
textField.setText(Arrays.toString(buffer));
with this :
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String sTemp1 = "", sTemp2 = "";
while ((sTemp1 = br.readLine()) != null) {
sTemp2 = sTemp1;
}
in.close();
textField.setText(sTemp2);
I hope it will help you.

Reading single InputStream from multiple methods

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();
}

Socket Programming file upload issues?Complete file not uploading

Hi i am using the following code for uploding my file from android phone to the server bt the file does not upload completely..e.g i uploded a 11kb file and got only 8kb file at the server.What am i doing wrong?
Client side
Socket skt = new Socket"112.***.*.**", 3000);
String FileName=fil.getName();
PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())),true);
out2.println("Upload");
out2.println(FileName);
out2.println(spinindx);
out2.println(singleton.arrylst_setngs.get(0).toString());
out2.println(singleton.arrylst_setngs.get(1).toString());
out2.println(singleton.arrylst_setngs.get(2).toString());
out2.println(singleton.arrylst_setngs.get(3).toString());
out2.println(singleton.arrylst_setngs.get(4).toString());
out2.flush();
//Create a file input stream and a buffered input stream.
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
out.write(buf,0,i);
publishProgress(in.available());
System.out.println(i);
}
//Close the writers,readers and the socket.
in.close();
out.flush();
out.close();
out2.close();
skt.close();
}
catch( Exception e ) {
System.out.println(e);
}
The server side
InputStream inStream = socket.getInputStream();
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
String Request=inm.readLine();
if(Request.equals("Upload")){
fileName = inm.readLine();
chosn = inm.readLine();
lt=inm.readLine();
cs = inm.readLine();
om = inm.readLine();
o = inm.readLine();
check=inm.readLine();
//Read, and write the file to the socket
BufferedInputStream in = new BufferedInputStream(inStream);
int i=0;
File f=new File("D:/data/"+filePrefx+fileName);
if(!f.exists()){
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream("D:/data/"+filePrefx+fileName);
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
System.out.println(i);
out.write(buf,0,i);
System.out.println("Receiving data...");
}
in.close();
inStream.close();
out.close();
fos.close();
socket.close();
Looks like you are using both a BufferedReader and a BufferedInputStream on the same underlying socket at the server side, and two kinds of output stream/writer at the client. So your BufferedReader is buffering, which is what it's supposed to do, and thus 'stealing' some of the data you're expecting to read with the BufferedInputStream. Moral: you can't do that. Use DataInputStream & DataOutputStream only, and writeUTF()/readUTF() for the 8 lines you are reading from the client before the file.
You shared the same underlying InputStream between your BufferedReader and bufferedInputStream.
What happened is, when you do the reading through BufferedReader, it reads more than the a few lines you requested from the underlying InputStream into its own internal buffer. And when you create the BufferedInputStream, the data has already been read by the BufferedReader. So Apart from what EJP suggested not to use any buffered class, you can create the BufferedInputStream, and then create the Reader on Top of it. The code is something like this:
BufferedInputStream in = new BufferedInputStream(inStream);
Reader inm = new InputStreamReader(in);
Add it to the beginning of your server code and remove this line:
BufferedInputStream in = new BufferedInputStream(inStream);
See this, i never tried though
void read() throws IOException {
log("Reading from file.");
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine() + NL);
}
}
finally{
scanner.close();
}
log("Text read in: " + text);
}
Shamelessly copied from
http://www.javapractices.com/topic/TopicAction.do?Id=42

Categories

Resources