Java Writer stopping after single write - java

I'm looking to read the contents of a URL and write them to file, this is working as expected but it's only writing it a single time even though the program console shows multiple lines.
Code:
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
while(true) {
URL oracle = new URL("https://linkToData.com");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
writer.println(inputLine);
System.out.println(inputLine);
}
writer.close();
The data in the URL refreshes constantly so there should be different data each time as the console print shows but it's only writing the first instance to file.

The key is writer.close()! If you want to write the file anew every time, you have to reopen the Writer each time.

If you want to append file each time you have to flush writer instead of closing and close at the end.
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
while(condition) {
URL oracle = new URL("https://linkToData.com");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
writer.println(inputLine);
System.out.println(inputLine);
}
writer.flush();
}
writer.close();

Related

How can I write my exported XML to an XML File?

My job is to search for a book on WikiBooks and export the corresponding XML file. This file should be edited later. However, the error occurs earlier.
My idea was to read the page and write it line by line to an XML file. Here is my code for it:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welches Buch wollen Sie suchen?");
book = (reader.readLine());
book = replaceSpace(book);
URL url = new URL("https://de.wikibooks.org/wiki/Spezial:Exportieren/" + book);
URLConnection uc = url.openConnection();
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
uc.connect();
BufferedReader xmlReader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
File file = new File("wiki.xml");
FileWriter writer = new FileWriter(file);
String inputLine;
while ((inputLine = xmlReader.readLine()) != null) {
writer.write(inputLine + "\n");
}
xmlReader.close();
The code is executed without an error message, but the saved file ends in the middle of a word and is therefore incomplete.
How can I work around this problem?
As the comment suggested the problem is that the content of the stream is not flushed to the file. If you call close() on your writer the content is automatically flushed to the file.
Here is your code with the added statement in the end:
BufferedReader xmlReader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
File file = new File("wiki.xml");
FileWriter writer = new FileWriter(file);
String inputLine;
while ((inputLine = xmlReader.readLine()) != null) {
writer.write(inputLine + "\n");
}
writer.close();
xmlReader.close();
A much easier solution that is built into Java is using the Files class. My suggestion is that you replace the above code with the following simple statement, which directly stores your InputStream into a file and automatically takes care of the streams.
Files.copy(uc.getInputStream(), Paths.get("wiki.xml"), StandardCopyOption.REPLACE_EXISTING);

Write data to an external file in Java

I have data in my BufferedReader, but I want to print the data in an external file.
How do I do that?
URL url=new URL(input);
BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while((inputLine=br.readLine())!=null)
System.out.println(inputLine);
br.close();
The above code works for me. Instead, I want to save the data in an external file.
URL url=new URL(input);
BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
BufferedWriter writer = Files.newBufferedWriter(Paths.get(""));
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
writer.write(inputLine);
// must do this: .readLine() will have stripped line endings
writer.newLine();
}
writer.close();
br.close();
If i have understood correctly you have your data and you want to write them in an external txt file.
String path1 = "Your Path File";
File file1 = new File(path1);
file1.createNewFile();
FileWriter fw = new FileWriter(file1);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("\n"+ your data); // "\n" because the new data will overwrite the previous
//and it will be lost
bw.flush();
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
br.close();
bw.close();

BufferedReader and BufferedWriter with Socket

geniuses.
I want to used socket in Java.
Here is a part of my server side code:
ServerSocket ss = new ServerSocket(this.portNum);
while (!ss.isClosed()) {
Socket socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("reading");
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("read");
System.out.println("writing");
bw.write(this.wsp.parse(new String(sb.toString())).toJSONString());
bw.newLine();
bw.flush();
System.out.println("wrote");
bw.close();
br.close();
socket.close();
}
ss.close();
And my client side (test) code is:
Socket socket = new Socket("143.248.135.60", 44450);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("writing");
bw.write(str);
bw.newLine();
bw.flush();
System.out.println("wrote");
System.out.println("reading");
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("read");
br.close();
bw.close();
socket.close();
Both sides halt after printing "reading."
What's wrong with my codes?
Thank you for your help in advance!
Your application blocks at the servers site at this point:
while ((line = br.readLine()) != null) {
sb.append(line);
}
Your servers will read lines until the source stream gets closed (br.readLine() will return null when the end of stream has been reached). But this doesn't happen. It seems that your're expecting just a single line here so try this instead of the loop at the server side:
System.out.println("reading");
String line = br.readLine();
System.out.println("read");
Now about the name loop on the client side: The server will close the streams and the socket immediately after it has written its own data. So br.readLine() will return null on the client side after the first line was read. So it will do what you're expecting. But it will also works if you're replacing the code as I've suggested it for the server side.
Hope it helps.
Edit based on the clarification of the question (need to read multiple lines):
The easiest way based on your work is to use a control character like "End of transmission" (0x04 on ASCII).
Client code:
System.out.println("writing");
bw.write("Hello");
bw.newLine();
bw.write("World");
bw.newLine();
bw.write(0x04); // EOT control character
bw.newLine(); // This is needed for BufferedReader/Writer - even if we've used a EOT
bw.flush();
System.out.println("wrote");
Continued in the next commend...
Server code:
while ((line = br.readLine()) != null && !(line.length() > 0 && line.charAt(0) == 0x04)) {
sb.append(line).append(System.lineSeparator());
}
If you're not using ASCII or UTF8 please review your used encoding to choose the correct control character.

How can I print every line available from one BufferedReader before checking another BufferedReader?

PrintWriter out = new PrintWriter(DoDSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(DoDSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println(fromServer);
if ((fromUser= stdIn.readLine()) != null)
{
//System.out.println(fromUser);
out.println(fromUser);
}
}
In this code for a client, I've created a Print Writer and a buffered reader which communicate with a Server, I also have a separate reader which reads the System.in from the command line.
My problem at the moment is that if the server send the client a multi line string, I will have to press enter to receive each line. How can I edit this code so that every line is printed from the buffered reader from the server, before it checks what the user has typed, rather than checking after every individual line?
Why not do one loop after the other?:
PrintWriter out = new PrintWriter(DoDSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(DoDSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println(fromServer);
}
while ((fromUser = stdIn.readLine()) != null)
{
System.out.println(fromUser);
}

Reading InputStream as UTF-8

I'm trying to read from a text/plain file over the internet, line-by-line. The code I have right now is:
URL url = new URL("http://kuehldesign.net/test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
LinkedList<String> lines = new LinkedList();
String readLine;
while ((readLine = in.readLine()) != null) {
lines.add(readLine);
}
for (String line : lines) {
out.println("> " + line);
}
The file, test.txt, contains ¡Hélló!, which I am using in order to test the encoding.
When I review the OutputStream (out), I see it as > ¬°H√©ll√≥!. I don't believe this is a problem with the OutputStream since I can do out.println("é"); without problems.
Any ideas for reading form the InputStream as UTF-8? Thanks!
Solved my own problem. This line:
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
needs to be:
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
or since Java 7:
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
String file = "";
try {
InputStream is = new FileInputStream(filename);
String UTF8 = "utf8";
int BUFFER_SIZE = 8192;
BufferedReader br = new BufferedReader(new InputStreamReader(is,
UTF8), BUFFER_SIZE);
String str;
while ((str = br.readLine()) != null) {
file += str;
}
} catch (Exception e) {
}
Try this,.. :-)
I ran into the same problem every time it finds a special character marks it as ��. to solve this, I tried using the encoding: ISO-8859-1
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("txtPath"),"ISO-8859-1"));
while ((line = br.readLine()) != null) {
}
I hope this can help anyone who sees this post.
If you use the constructor InputStreamReader(InputStream in, Charset cs), bad characters are silently replaced. To change this behaviour, use a CharsetDecoder :
public static Reader newReader(Inputstream is) {
new InputStreamReader(is,
StandardCharsets.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
);
}
Then catch java.nio.charset.CharacterCodingException.

Categories