这是我写关于文件写入小程序
import java.io.*;
public class FileDemo {
public void writeFile(){
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter string");
String str=new String();
try{
str=ds.readLine();
}
catch(IOException e){
e.getStackTrace();
}
try{
String string=new String();
string=new FileDemo().ReadFile();
DataOutputStream o=new DataOutputStream(new FileOutputStream("d:/java/text.txt"));
try{
System.out.println(str);
o.writeBytes(string+","+str+ "\r\n");
}
catch(IOException e2){
System.out.println("文件写入失败");
}
}
catch(FileNotFoundException e1){
System.out.println("文件不存在");
}
}
public static void readFile(){
try{
BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("d:/java/text.txt")));
String str,string;
try{
str=d.readLine();
System.out.println(str);
}
catch(IOException e){
System.out.println("文件读出失败");
}
}
catch(Exception e1){
e1.getStackTrace();
}
}
public String ReadFile(){
String str=new String();
try{
BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("d:/java/text.txt")));
try{
str=d.readLine();
System.out.println(str);
}
catch(IOException e){
System.out.println("文件读出失败");
}
}
catch(Exception e1){
e1.getStackTrace();
}
return str;
}
public static void main(String[] args) {
new FileDemo().writeFile();
readFile();
}
}
上面的程序中基本解决了向文件写入新的字符串会覆盖原有的字符串的问题,但是如果字符串很长,会占有很多的内存空间,这个问题怎么解决呢? 如果有更完美的办法,请各位大侠将自己的原码一览,
谢谢,
import java.io.*;
public class FileDemo {
public void writeFile(){
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter string");
String str=new String();
try{
str=ds.readLine();
}
catch(IOException e){
e.getStackTrace();
}
try{
String string=new String();
string=new FileDemo().ReadFile();
DataOutputStream o=new DataOutputStream(new FileOutputStream("d:/java/text.txt"));
try{
System.out.println(str);
o.writeBytes(string+","+str+ "\r\n");
}
catch(IOException e2){
System.out.println("文件写入失败");
}
}
catch(FileNotFoundException e1){
System.out.println("文件不存在");
}
}
public static void readFile(){
try{
BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("d:/java/text.txt")));
String str,string;
try{
str=d.readLine();
System.out.println(str);
}
catch(IOException e){
System.out.println("文件读出失败");
}
}
catch(Exception e1){
e1.getStackTrace();
}
}
public String ReadFile(){
String str=new String();
try{
BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("d:/java/text.txt")));
try{
str=d.readLine();
System.out.println(str);
}
catch(IOException e){
System.out.println("文件读出失败");
}
}
catch(Exception e1){
e1.getStackTrace();
}
return str;
}
public static void main(String[] args) {
new FileDemo().writeFile();
readFile();
}
}
上面的程序中基本解决了向文件写入新的字符串会覆盖原有的字符串的问题,但是如果字符串很长,会占有很多的内存空间,这个问题怎么解决呢? 如果有更完美的办法,请各位大侠将自己的原码一览,
谢谢,
解决方案 »
- 如何中断正在运行的非阻塞线程?
- 怎么限制JTextarea的字数
- 类File的问题,求助
- MyEclipse无法发布web工程
- java中边框的问题
- 处理文本框事件出错(付实例及错误提示)
- 请大家讨论一下数学功底与编程能力(美国的程序员)
- 如何把Content-Type: application/octet-stream的"=?gb2312?B?seCzzMu8z+u1xNPW0ru0zrjvw/w=?="转化为中文.
- JAVA中找不到符号的问题。
- 初学者,不知道这是怎么了,是环境变量不对还是什么
- 关于文件处理API中的更改文件名的函数renameTo()问题!----Rename有时失败!
- 如何用Class.forName方法动态加载构造方法带参数的类?
使用追加方式向文件中写入新的字符串