日期:2014-05-20  浏览次数:21094 次

java中如何把窗体的文本框中输入的内容保存到.txt文件中去,不要用数据库
把窗体文本框中输入的内容保存到.txt文件中

------解决方案--------------------
把文本框中的内容通过IO流写到文件中去就行啦!~
------解决方案--------------------
Java code

    public static void main(String[] args){
        FileWriter writer = null;
        try {
            writer = new FileWriter("你需要写的文件的url");
            writer.write("内容");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

------解决方案--------------------
package com.chm;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class AA {
String fileName="F:\\XX.txt";
public static void main(String[] args) throws IOException {
AA aa=new AA();
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
//将控制台输入的内容写入.txt文件
aa.writeTxt(in1.readLine());
//读取.txt文件的内容打印到控制台
System.out.println(aa.readTxt());
}
/*向txt文件中写内容*/
public void writeTxt(String str){
File f = new File(fileName);
try {
if(!f.exists()||!f.isFile()){
f.createNewFile();
}
OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
outputStream.write(str);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();

}
/*读取txt文件的内容*/
public String readTxt(){
File file = new File(fileName);
String prompt="";
try{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String str=null;
while((str=br.readLine())!=null){
prompt=prompt+str;
}
br.close();
}catch(Exception e) {
e.printStackTrace();
}
return prompt; 
}
}

------解决方案--------------------
就用Writer类就可以了啊
将内容写进File里面
再通过Writer写出来
------解决方案--------------------
如果是窗体的话 就写一个button事件
获取窗体输入的文字然后用3楼的方法write进去
------解决方案--------------------
直接读取文本数据,用java的流创建文件,write写入文件。。。