Originariamente inviato da misterwolf
andrea mi sapresti indicare un buon tutorial che utilizzi il metodo da te citato cioè filewriter?
non sono riuscito a trovarne.
credo che lo creerò da solo il file rss...è appartente ad una tipologia non proprio comune.
ti ringrazio!
Se n'è parlato miliardi di volte anche qui sul forum. Il sito dedicato a java su html.it è un'utilissima fonte e java.sun.com un must da tenere sempre a portata di click quando si programma in java.
Ciò detto, se ti va di reinventare la ruota, ti propongo due soluzioni: una puro JSP e l'altra JSP + Bean. A te codificare una possibile terza, tramite Servlet.
solo JSP
codice:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*;" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Writer Demo</title>
</head>
<body>
<h2>Questa pagina scrive un file</h2>
<%
String path = "C:/Documents and Settings/Andrea/Desktop/";
String fileName = "fromJSP.txt";
File f = new File(path, fileName);
String contents = "Questo viene scritto alle: "+(new java.util.Date());
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(f));
buf.write(contents);
buf.flush();
buf.close();
}
catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
JSP + Bean
codice:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Writer Demo - con Bean</title>
</head>
<body>
<h2>Questa pagina scrive un file con un Bean</h2>
<jsp:useBean class="beans.FileWriterBeans" id="fileWrite" ></jsp:useBean>
<%
String path = "C:/Documents and Settings/Andrea/Desktop/";
String fileName = "fromJSP.txt";
String contents = "Questo viene scritto da un bean alle: "+(new java.util.Date());
fileWrite.setPath(path); fileWrite.setFileName(fileName);
fileWrite.setContents(contents);
fileWrite.write();
%>
</body>
</html>
codice:
package beans;
import java.io.*;
/**
*
* @author Andrea
*/
public class FileWriterBeans {
private String path, fileName, contents;
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getContents() {
return this.contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public FileWriterBeans() {
}
public void write() {
try {
BufferedWriter buw = new BufferedWriter(new FileWriter(new File(this.path, this.fileName)));
buw.write(this.contents);
buw.flush();
buw.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}