这是我写关于文件写入小程序
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();
}
}
上面的程序中基本解决了向文件写入新的字符串会覆盖原有的字符串的问题,但是如果字符串很长,会占有很多的内存空间,这个问题怎么解决呢? 如果有更完美的办法,请各位大侠将自己的原码一览,
谢谢,
解决方案 »
- 一个小程序
- 增加和删除节点的问题
- 用weblogic和java 开发web程序调试的时候发现如下错误信息,请问可能是什么问题?
- 求助,初学者一个关于数组的问题
- 求计算素数的算法,好象在IBM看见过,现在找不到,在线等待。
- 哪位有时间控件或者是Applet(能够在java Application中使用)
- list.get(0) 和list.get(i)
- jBuilder中的中文问题
- 请各位高手帮帮忙,本人在此先谢过了!请…………帮帮忙!!!
- 热烈庆祝,我们实现了jdk1.3的本地下载!
- 关于文件处理API中的更改文件名的函数renameTo()问题!----Rename有时失败!
- 如何用Class.forName方法动态加载构造方法带参数的类?
使用追加方式向文件中写入新的字符串