Helpful Information
 
 
Category: Java Help
Writing on Server

How can I write information into a file on my server in java servlets?

go to the Java API (http://java.sun.com/j2se/1.3/docs/api/index.html) 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 (http://java.sun.com/products/servlet/2.2/javadoc/index.html). 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.

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.

what is the behavior? are there any error messages?

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.

how about this:


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.










privacy (GDPR)