|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
How can I write information into a file on my server in java servlets?
|
|
#2
|
||||
|
||||
|
go to the Java API and look up the java.io package. you write to a file from a servlet the same way you write to a file from any other java code (except applets). i advise using a FileWriter wrapped in a BufferedWriter. good luck. here's another link to the servlet API. the APIs are your best friends. when i have a question about how to implement something, i go there and 90% of the time I find the answer.
|
|
#3
|
|||
|
|||
|
I've already tryed a FileWriter wrapped in a BufferOutputStream. I have the following code:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.net.*; public class MindmapWWWBoard extends HttpServlet { String username; String message; private Writer out2; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); postReceipt(request, out); } private void postReceipt(HttpServletRequest request, PrintWriter out) { username = request.getParameter("username"); message = request.getParameter("message"); out.println("<html>n" + "<head>n" + "<title>n" + "Your Postn" + "</title>n" + "</head>n" + "<body>n" + "<b>Username:</b> " + username + "<br>" + "<b>Message:</b> " + message + "</body>n" + "</html>"); try { FileWriter fw = new FileWriter("mindmap.html", true); BufferedWriter out2 = new BufferedWriter(fw); out2.write(message); out2.flush(); out2.close(); } catch (IOException e) { } } } Does anyone know why this isn't working? Thanks in advance. |
|
#4
|
||||
|
||||
|
what is the behavior? are there any error messages?
|
|
#5
|
|||
|
|||
|
It compiles ok, but when I run it on the server, it doensn't write to the HTML file. It shows the username and message after post.
|
|
#6
|
||||
|
||||
|
how about this:
Code:
File file = new File("/home/your/path", "mindmap.html");
byte[] fileBuffer;
FileOutputStream outstream;
FileInputStream instream;
int fileLength;
if (file.exists()) fileLength=(int)file.length();
else fileLength=0;
// buffer for your message + file's contents
fileBuffer = new byte[message.length() + fileLength];
// stream in the file to the buffer
if (file.exists()) {
try {
inStream = new FileInputStream(file);
int numRead = 0;
int counter = 0;
while (numRead!=-1 && counter<fileLength) {
numRead = inStream.read(fileBuffer, counter, fileLength-counter);
counter += numRead;
}
}
catch...
}
byte[] messageBytes = message.getBytes();
//add message to the file buffer
for (int j=fileLength; j<fileBuffer.length; j++)
fileBuffer[j] = messageBytes[j-fileLength];
//shove fileBuffer back into file
try {
outStream = new FileOutputStream(file);
outStream.write(fileBuffer);
outStream.close();
}
catch...
that's bound to have a bug or two but i'm sure you can fix it up. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Writing on Server |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|