一、java中如何实现数据的保存,以供下次使用???如何保存,跪求??
给你个例子看看,保存到文件的
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class $ {
public static void main(String[] args) {
String path = d:/test.txt;
try {
save(path);
read(path);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void save(String path) throws Exception {
Bean bean = new Bean();
bean.setName(root);
bean.setPassword(123456);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(path)));
oos.writeObject(bean);
oos.flush();
oos.close();
}
private static void read(String path) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(path)));
Bean bean = (Bean) ois.readObject();
System.out.println(bean.getName());
System.out.println(bean.getPassword());
}
}
class Bean implements Serializable {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
二、java如何实现文本保存??
try{ FileOutputStream fos=new FileOutputStream(test.txt,true);//true表明会追加内容 PrintWriter pw=new PrintWriter(fos); pw.write(你想写入的内容); pw.flush(); }catch(FileNotFoundException e){ e.printStackTrace(); }finally{ try{ pw.close(); }catch(Exception e){ e.printStackTrace(); } }
三、java对象如何保存在本地文件或在网络上传输
仅供参考:原理-对象要先实现序列化接口,然后在通过字节流输入到硬盘或者网络传输
具体代码-这个应该可以百度的到
四、如何用JAVA编程实现抓取某一页面的内容并保存成本地文件?
import java.io.*;
import java.net.*;
public class server
{
public static void main(String[] args)
{
System.out.println(server starting.......);
try{
serversocket server=new serverSocker(7777);//listen7777port
Socket st=accpet();
BufferdReader br_net=new BufferdReader(new InputStreamReader)
BufferdReader br_key=new BufferdReader(new InputStreamReader)
printWriter pw=new PrintWriter(st.getOutputStream());
while(true){
String a=br_key.readLine();
pw.println(D:\);
pw.flush();
s=br_net.readLine();
system.out.println(hesays+s);
}
}catch(Exception e){}
|