Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing a new line character  RSS feed

 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple program that opens a URL and copies that file into a temp file in the same directory the program is in. The problem is that the temp file doesn't output the new line characters. It just makes the file one really long line. Does anyone know how to read the new line and write it back to make my files look nice.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This could be the subclass of InputStream you are using or it could be how the server is writing this information. Could you post a section of code for us to look at?
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Noah,
In Unix the newLine is represented by only '\n' .In WIN it is \r\n. Please make a small change like in the following code and check.
regds
maha anna

[This message has been edited by maha anna (edited December 08, 2000).]
 
Brent Worden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And for OS neutral Java, the newline character(s) is:

------------------
Brent Worden
http://www.Brent.Worden.org/
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brent,
If these input and output streams only use integers, then how am I supposed to write out a string or compare a string to and integer. Also, if it a windows based new line character you have to read in the \r and then the \n. That is two characters to read, so it makes it even more messy.
 
sathish297 kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
Try this piece of code in your program.
1.First create a PrintWriter class.

PrintWriter pow=new PrintWriter(op,true);
2. Next wherever you want a newline add this line.

pow.println();
 
Brent Worden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm,
ENDL.getBytes() will return a byte array that can be output with an OutputStream.
Another suggestion would be to use Reader and Writer objects instead of InputStream and OutputStream objects.

Since BufferedReader has a readLine() method and, as sathish pointed out, PrintWriter has a println() method, implementing the copying becomes trivial


------------------
Brent Worden
http://www.Brent.Worden.org/
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I came across your program. It is working but data is being represented like a garbage file.
As you used BufferReader so it can be read each line. So if you use StreamTokenizer you can represent you data line by line. I compiled your program and run it through using Token.class file and it has been represented line by line. Kindly Checked the Topic "Tokenized data" Starter is Mr. Savio Mascarenhas last updated: Jan, 07 2001. There you will find a java source file named Token. Kindly compile that file and then see your Temp.txt file i.e. the file you are creating using your program. You will find your data line by line.
So it is better to change your file by using Token.java file's
content.
Hahh....
 
titus126
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try ouyt this code
Hope this is the one u r looking for
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TryServlet extends HttpServlet {
ServletContext m_oContext = null;
public void init(ServletConfig p_oConfig) throws ServletException
{
m_oContext = p_oConfig.getServletContext();
super.init(p_oConfig);
}
public void doGet(HttpServletRequest req, HttpServletResponse res){
try{
BufferedOutputStream log = new BufferedOutputStream(new FileOutputStream("c:/dev/load.doc", true));
log.write("beginning".getBytes());
res.setContentType( "application/msword" );
log.write("beginning header".getBytes());
res.setHeader("Content-disposition", "attachment; filename= rich1.rtf" );
byte[] buff = new byte[2048];
ServletOutputStream out = res.getOutputStream();
String fileURL = "/rich.rtf" ;
log.write("beginning url".getBytes());
log.write(req.getServletPath().getBytes());
FileInputStream url = new FileInputStream(m_oContext.getRealPath(fileURL));
BufferedOutputStream bos = new BufferedOutputStream(out);
BufferedInputStream bis = new BufferedInputStream(url);
int bytesRead;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
log.write(buff, 0, bytesRead);
bos.write(buff, 0, bytesRead);
}
bos.close();
bis.close();
log.close();
}
catch(IOException e){
}
}
}

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!