SunQuest
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old January 25th, 2001, 04:38 PM
kenowhere kenowhere is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 34 kenowhere User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Question

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

Reply With Quote
  #2  
Old January 25th, 2001, 06:22 PM
merkinmuffley's Avatar
merkinmuffley merkinmuffley is offline
film at 11
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Portland, OR
Posts: 413 merkinmuffley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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.

Reply With Quote
  #3  
Old January 25th, 2001, 09:37 PM
kenowhere kenowhere is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 34 kenowhere User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Question

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.

Reply With Quote
  #4  
Old January 26th, 2001, 10:05 AM
merkinmuffley's Avatar
merkinmuffley merkinmuffley is offline
film at 11
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Portland, OR
Posts: 413 merkinmuffley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
what is the behavior? are there any error messages?

Reply With Quote
  #5  
Old January 26th, 2001, 10:17 AM
kenowhere kenowhere is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 34 kenowhere User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Cool

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.

Reply With Quote
  #6  
Old January 26th, 2001, 12:19 PM
merkinmuffley's Avatar
merkinmuffley merkinmuffley is offline
film at 11
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Portland, OR
Posts: 413 merkinmuffley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Writing on Server


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway