public void writefiles(String path) throws IOException
{
try
{
String s = path;
File nf = new File(path);
if(!nf.exists())
{
nf.createNewFile();
}
String tx ="";
FileWriter fw = new FileWriter(s,true);
String nextLine = System.getProperty("line.separator");
fw.write("Hello");
fw.close();
}catch(Exception e)
{}
}
{
try
{
String s = path;
File nf = new File(path);
if(!nf.exists())
{
nf.createNewFile();
}
String tx ="";
FileWriter fw = new FileWriter(s,true);
String nextLine = System.getProperty("line.separator");
fw.write("Hello");
fw.close();
}catch(Exception e)
{}
}
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String strContent = "中文测试".toString();
myFile.println(strContent);
resultFile.close();
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;public class test{
public static void main(String args[]){
try{
String filePath="c:/lixiang.txt";
File myFilePath=new File(filePath.toString());
if(!myFilePath.exists()){
myFilePath.createNewFile();
}
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String strContent="Hello my first content!";
myFile.println(strContent.toString());
resultFile.close();
}catch(IOException e){
System.out.println("error");
}
}
}
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<html>
<body>
<%! //Create one table
Hashtable hashtable=new Hashtable();
synchronized void putGoodsToHashtable(String key,String list)
{
hashtable.put(key,list);
}
synchronized void removeGoodsToHashtable(String key)
{
hashtable.remove(key);
}
%>
<%--Get name and amount --%>
<%
String name=request.getParameter("N");
if(name==null)
{
name="have no any goods number";
}
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
String mount=request.getParameter("M");
if(mount==null)
{
mount="have no any recorder";
}
byte d[]=name.getBytes("ISO-8859-1");
name=new String(c);
%>
<%--if not exist ,you are first one --%>
<%
File f=new File("F:/temp","goods_name.txt");
if(f.exists())
{
try{
FileInputStream in=new FileInputStream(f);
ObjectInputStream object_in=new ObjectInputStream(in);
hashtable=(Hashtable)object_in.readObject();
object_in.close();
in.close();
String temp=(String)hashtable.get(name);
if(temp==null)
{
temp="";
}
if((temp.length())!=0)
{
out.print("<BR>"+"This goods ID is exist,you update");
String s="Goods ID:"+name+" ,mount: "+mount;
removeGoodsToHashtable(name);
putGoodsToHashtable(name,s);
try{
FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
}catch(Excaption eee){
}
}
else{
String s="Goods ID:"+name+" ,mount: "+mount;
putGoodsToHashtable(name,s);
try{
FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
}catch(Excaption eee){
}
out.print("<BR>"+"you have saved ");
out.print("<BR>"+"Goods ID:"+name);
}
}
else{
String s="Goods ID:"+name+" ,mount: "+mount;
try{
FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
out.print("<BR>"+"you are the first one ");
out.print("<BR>"+"Goods ID:"+name);
}catch(Excaption eee){
}
}
%>
<form action="showgoods.jsp"method=post>
<p>Look up
<BR><input type=submit value="Goto">
</form>
<A href="Example4_11.jsp"><BR>Goback
</body>
</html>