当前位置: 技术问答>java相关
关于文件的写入,读出
来源: 互联网 发布时间:2015-06-01
本文导语: 怎样将一行字符串保存成一个文件(例如:note.txt) 然后在读出到textArea? 我用 FileOutputStream file = new FileOutputStream("note.txt"); BufferedOutputStream buff = new BufferedOutputStream(file); 不知道怎么赋值给buff? BufferedOutputStream...
怎样将一行字符串保存成一个文件(例如:note.txt)
然后在读出到textArea?
我用
FileOutputStream file = new FileOutputStream("note.txt");
BufferedOutputStream buff = new BufferedOutputStream(file);
不知道怎么赋值给buff?
BufferedOutputStream要的是byte类型的?
希望高手能给了例子贴出来学习
thanx!
然后在读出到textArea?
我用
FileOutputStream file = new FileOutputStream("note.txt");
BufferedOutputStream buff = new BufferedOutputStream(file);
不知道怎么赋值给buff?
BufferedOutputStream要的是byte类型的?
希望高手能给了例子贴出来学习
thanx!
|
读写文件的公式:
// 读 ---------------------------------------------------
String str = "";
String Data = "";
BufferedReader Reader = new BufferedReader(new FileReader("file.txt"));
while(true){
str = Reader.readLine();
if (str == null) break;
Data = str;
}
Reader.close();
// 写 ---------------------------------------------------
FileWriter Writer = new FileWriter("file.txt");
Writer.white("123456"+"n");
......
Writer.flush();
Writer.close();
//---------------------------------------------------
PS:记住要 catch Exception
// 读 ---------------------------------------------------
String str = "";
String Data = "";
BufferedReader Reader = new BufferedReader(new FileReader("file.txt"));
while(true){
str = Reader.readLine();
if (str == null) break;
Data = str;
}
Reader.close();
// 写 ---------------------------------------------------
FileWriter Writer = new FileWriter("file.txt");
Writer.white("123456"+"n");
......
Writer.flush();
Writer.close();
//---------------------------------------------------
PS:记住要 catch Exception
|
try{
FileOutputStream myS1=new FileOutputStream("d:testokmap.txt");
BufferedOutputStream Bout=new BufferedOutputStream(myS1);
DataOutputStream out1=new DataOutputStream(Bout);
out1.writeDouble(3.12123);
out1.writeBytes("It's ok!");
out1.close();
}catch(EOFException et) {
System.out.println("End of stream encountered");
}catch(FileNotFoundException et) {
System.out.println(
"File Not Found");
}catch(IOException et) {
System.out.println("IO Exception");
}
}
FileOutputStream myS1=new FileOutputStream("d:testokmap.txt");
BufferedOutputStream Bout=new BufferedOutputStream(myS1);
DataOutputStream out1=new DataOutputStream(Bout);
out1.writeDouble(3.12123);
out1.writeBytes("It's ok!");
out1.close();
}catch(EOFException et) {
System.out.println("End of stream encountered");
}catch(FileNotFoundException et) {
System.out.println(
"File Not Found");
}catch(IOException et) {
System.out.println("IO Exception");
}
}
|
public String readFile(String filename) throws Exception
{
//Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
try
{
breader = new BufferedReader(new FileReader(filename));//header
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}
catch(Exception e)
{
throw e;
}
return buf.toString();
}
{
//Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
try
{
breader = new BufferedReader(new FileReader(filename));//header
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}
catch(Exception e)
{
throw e;
}
return buf.toString();
}
|
public static void writeFile(String str, String filename) throws Exception
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile
|
try
{
String s=textArea.getText();//得到jtextarea的内容
File file=new File(".\test.txt");
FileWriter out=new FileWriter(file);
out.write(s);//将textArea的内容写到文件
out.close();//关闭
}
catch(IOException e)
......
这个片段就完成写文件了,很简单吧。自己做READ部分吧
{
String s=textArea.getText();//得到jtextarea的内容
File file=new File(".\test.txt");
FileWriter out=new FileWriter(file);
out.write(s);//将textArea的内容写到文件
out.close();//关闭
}
catch(IOException e)
......
这个片段就完成写文件了,很简单吧。自己做READ部分吧
|
既然是读写文件,就用FileReader和FileWiter这两个类拉
|
安装JB6没有啊?
感觉你想做一个文本编辑器之类的东西
如果是的话,可以看看JB的自带教程
里面有一个TextEditor的完整例子,可以读取,保存,另存文件,设置字体等。
step by step地教你做。
感觉你想做一个文本编辑器之类的东西
如果是的话,可以看看JB的自带教程
里面有一个TextEditor的完整例子,可以读取,保存,另存文件,设置字体等。
step by step地教你做。
|
.\test.txt好象是取得你的默认用户路径
而test.txt就是你这个程序文件的同级目录下
而test.txt就是你这个程序文件的同级目录下