Writing socket stream to file takes long - java

I have code which writes the inputstream from a socket to a file with a bufferedreader. This works, but for some reason it takes a long time for the bufferedwriter to finish writing to the file (multiple minutes, of which less than first 2 seconds are spent writing to the file). The code is as follows:
#Override
public void handleRequest() {
Socket s = null;
BufferedReader br = null;
Writer writer = null;
PrintWriter pw = null;
try {
s = new Socket(this.getHost(), this.getPort());
pw = new PrintWriter(s.getOutputStream());
pw.println(this.getHTTPCommand());
pw.println(this.getHostCommand());
pw.print(ENDOFREQUEST);
pw.flush();
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String currentDir = System.getProperty("user.dir");
String filePath = this.getPath();
if(this.getPath().equals("/")){
filePath = "/index.html";
}
Path completePath = Paths.get(currentDir, filePath);
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(completePath.toString()), "utf-8"));
String t;
while((t = br.readLine()) != null){
writer.write(t + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (writer != null) {
writer.close();
}
if (br != null) {
br.close();
}
if (pw != null) {
pw.close();
}
if (s != null) {
s.close();
}
}
catch (IOException e){
e.printStackTrace();
}
}
}

You probably need to flush the BufferedWriter after your while loop. It doesn't affect how your code works, but a try-with-resources would tidy up the code as well.

Related

Use try-with-resources or close this "BufferedReader" in a "finally" clause

Been looking for a way to fix this issue. Read all the previous answers but none helped me out.
Could it be any error with SonarQube?
public class Br {
public String loader(String FilePath){
BufferedReader br;
String str = null;
StringBuilder strb = new StringBuilder();
try {
br = new BufferedReader(new FileReader(FilePath));
while ((str = br.readLine()) != null) {
strb.append(str).append("\n");
}
} catch (FileNotFoundException f){
System.out.println(FilePath+" does not exist");
return null;
} catch (IOException e) {
e.printStackTrace();
}
return strb.toString();
}
}
You are not calling br.close() which means risking a resource leak. In order to reliably close the BufferedReader, you have two options:
using a finally block:
public String loader(String FilePath) {
// initialize the reader with null
BufferedReader br = null;
String str = null;
StringBuilder strb = new StringBuilder();
try {
// really initialize it inside the try block
br = new BufferedReader(new FileReader(FilePath));
while ((str = br.readLine()) != null) {
strb.append(str).append("\n");
}
} catch (FileNotFoundException f) {
System.out.println(FilePath + " does not exist");
return null;
} catch (IOException e) {
e.printStackTrace();
} finally {
// this block will be executed in every case, success or caught exception
if (br != null) {
// again, a resource is involved, so try-catch another time
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return strb.toString();
}
using a try-with-resources statement:
public String loader(String FilePath) {
String str = null;
StringBuilder strb = new StringBuilder();
// the following line means the try block takes care of closing the resource
try (BufferedReader br = new BufferedReader(new FileReader(FilePath))) {
while ((str = br.readLine()) != null) {
strb.append(str).append("\n");
}
} catch (FileNotFoundException f) {
System.out.println(FilePath + " does not exist");
return null;
} catch (IOException e) {
e.printStackTrace();
}
return strb.toString();
}
Seems like you just want to read all lines from a file. You could use this:
public String loader(String FilePath) {
try(Scanner s = new Scanner(new File(FilePath).useDelimiter("\\A")) {
return s.hasNext() ? s.next() : null;
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
The code you wrote is indeed leaking resources as you're not closing your BufferedReader. The following snippet should do the trick:
public String loader(String filePath){
String str = null;
StringBuilder strb = new StringBuilder();
// try-with-resources construct here which will automatically handle the close for you
try (FileReader fileReader = new FileReader(filePath);
BufferedReader br = new BufferedReader(fileReader);){
while ((str = br.readLine()) != null) {
strb.append(str).append("\n");
}
}
catch (FileNotFoundException f){
System.out.println(filePath+" does not exist");
return null;
}
catch (IOException e) {
e.printStackTrace();
}
return strb.toString();
}
If you're still having issues with this code, then yes, it's SonarQubes fault :-)

Java, copy from a file to another, line by line with an interval

I have a file with 120 lines and I want to move them one by one to another file with an interval for example of 1 seconds and to be able to find after 10 seconds 10 lines in the new file.
But for my case, I execute the program with 0 lines in the new files until the end, and then I find the data.
String sourceFileName = "D:\\oldfile.txt";
String destinationFileName = "D:\\newfile.txt";
if(evt.getSource() == btnProcess)
{
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader(sourceFileName));
pw = new PrintWriter(new FileWriter(destinationFileName));
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
Thread.sleep(1000);
}
br.close();
pw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
Second, for 4 files to process in the same moment with different interval, I need to use Threads ?
Thanks for your help.
When you are writing to a text file, PrintWriter does not write it to disk immediately. Instead, it keeps the data in a buffer in memory.
You could manually flush the buffer to when you need data to be on disk. Just after println() call flush() as below.
while ((line = br.readLine()) != null) {
pw.println(line);
pw.flush();
Thread.sleep(1000);
}
You can call a
pw.flush();
directly after
pw.println(line);
This should do the trick.
As for your second part, you could do something like this, if you do not want to use threads:
public static void main(final String[] args) {
FileCopyDto[] files = new FileCopyDto[] {
new FileCopyDto("D:\\oldfile.txt", "D:\\newfile.txt", 5),
new FileCopyDto("D:\\oldfile2.txt", "D:\\newfile2.txt", 1)
};
try {
boolean dataAvailable = true;
int secondCount = 0;
while (dataAvailable) {
dataAvailable = false;
for (FileCopyDto d : files) {
d.write(secondCount);
dataAvailable = dataAvailable || d.isDataAvailable();
}
secondCount++;
Thread.sleep(1000);
}
for (FileCopyDto d : files) {
d.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
static class FileCopyDto {
String sourceFileName;
String destinationFileName;
int timeInSeconds;
BufferedReader br = null;
PrintWriter pw = null;
String nextLine;
public FileCopyDto(final String sourceFileName,
final String destinationFileName,
final int timeInSeconds) {
this.sourceFileName = sourceFileName;
this.destinationFileName = destinationFileName;
this.timeInSeconds = timeInSeconds;
}
public void open() throws IOException {
br = new BufferedReader(new FileReader(sourceFileName));
pw = new PrintWriter(new FileWriter(destinationFileName));
}
public boolean isDataAvailable() throws IOException {
if (br == null) {
open();
}
return (nextLine == null) || ((nextLine = br.readLine()) != null);
}
public void write(final int secondCount) {
if (nextLine != null && secondCount % timeInSeconds == 0) {
pw.println(nextLine);
pw.flush();
nextLine = null;
}
}
public void close() throws IOException {
br.close();
pw.close();
br = null;
}
}

Making Java I / O and change the file to split in java

I'm making a project where using java I / O
I have a file with the following data:
170631|0645| |002014 | 0713056699|000000278500
155414|0606| |002014 | 0913042385|000001220000
000002|0000|0000|00000000000|0000000000000000|000000299512
and the output I want is as follows:
170631
0645
002014
file so that the data will be decreased down
and this is my source code:
public class Tes {
public static void main(String[] args) throws IOException{
File file;
BufferedReader br =null;
FileOutputStream fop = null;
try {
String content = "";
String s;
file = new File("E:/split/OUT/Berhasil.RPT");
fop = new FileOutputStream(file);
br = new BufferedReader(new FileReader("E:/split/11072014/01434.RPT"));
if (!file.exists()) {
file.createNewFile();
}
while ((s = br.readLine()) != null ) {
for (String retVal : s.split("\\|")) {
String data = content.concat(retVal);
System.out.println(data.trim());
byte[] buffer = data.getBytes();
fop.write(buffer);
fop.flush();
fop.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I want is to generate output as above from the data that has been entered
File Input -> Split -> File Output
thanks :)
I think you forgot to mention what problem are you facing. Just by looking at the code it seems like you are closing the fop(FileOutputStream) every time you are looping while writing the split line. The outputStream should be closed once you have written everything, outside the while loop.
import java.io.*;
public class FileReadWrite {
public static void main(String[] args) {
try {
FileReader inputFileReader = new FileReader(new File("E:/split/11072014/01434.RPT"));
FileWriter outputFileWriter = new FileWriter(new File("E:/split/11072014/Berhasil.RPT"));
BufferedReader bufferedReader = new BufferedReader(inputFileReader);
BufferedWriter bufferedWriter = new BufferedWriter(outputFileWriter);
String line;
while ((line = bufferedReader.readLine()) != null) {
for (String splitItem : line.split("|")) {
bufferedWriter.write(splitItem + "\n");
}
}
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Writing to a file in Java. Help me code

I am passing a file path to this method which writes the in txt file. But when I run this program it is not writing full and I don't know where I made mistake.
public void content(String s) {
try {
BufferedReader br=new BufferedReader(new FileReader(s));
try {
String read=s;
while((read = br.readLine()) != null) {
PrintWriter out = new PrintWriter(new FileWriter("e:\\OP.txt"));
out.write(read);
out.close();
}
} catch(Exception e) { }
} catch(Exception e) { }
}
You shouldn't create your PrintWriter inside the loop every time:
public void content(String s) {
BufferedReader br=new BufferedReader(new FileReader(s));
try {
PrintWriter out=new PrintWriter(new FileWriter("e:\\OP.txt"));
String read=null;
while((read=br.readLine())!=null) {
out.write(read);
}
} catch(Exception e) {
//do something meaningfull}
} finally {
out.close();
}
}
Aditionally, as others have mentioned add a finally block, do not silently catch the exception, and follow the Java Coding Conventions.
close your PrintWriter inside finally block out side the loop
finally {
out.close();
}
It's better to use Apache Commons IO instead.
http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html should make the trick.
(Unless you are trying to learn the low-level stuff or actually knows why you can't use IOUtils for this case.)
try this
public void content(String s) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(s));
PrintWriter pr = new PrintWriter(new File("e:\\OP.txt"))) {
for (String line; (line = br.readLine()) != null;) {
pr.println(line);
}
}
}
Your closing stream before finishing it. So either put it into
<code>
finally {
out.close();
}
</code>
or see this simple example
<code>try {
String content = s;
File file = new File("/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
</code>

Java reads ANSI file incorrectly

I tried to read an ANSI encoded Arabic file in Java using the following two way
Scanner scanner = null;
try {
scanner = new Scanner(new File("test/input.txt"), "ISO-8859-6");
while (scanner.hasNextLine()) {
String input =scanner.nextLine();
processString(input);
}
I tried also to read with default encoding (i.e. I omitted the "ISO-8859-6")
Any suggestions?
Try this code:
public static void transform(File source, String srcEncoding, File target, String tgtEncoding) throws IOException {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(source), Charset.forName(srcEncoding)));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target), tgtEncoding));
char[] buffer = new char[16384];
int read;
while ((read = br.read(buffer)) != -1) {
bw.write(buffer, 0, read);
}
} finally {
try {
if (br != null) {
br.close();
}
} finally {
if (bw != null) {
bw.close();
}`enter code here`
}
}
}
Look at this:
private static final String FILENAME = "/Users/jucepho/Desktop/ansi.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
This file has this characters http://www.alanwood.net/demos/ansi.html

Categories