如何向文本文件中空格处插入文字?

shloshlo 2003-10-10 11:04:10

文本文件的每行的格式是这样的:abc ef ghijk (各段长度未知)
执行程序后希望变为比如说:first_abc,second_ef,third_ghijk,end.
请问如何用java程序实现?
...全文
247 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangFrame 2003-10-14
  • 打赏
  • 举报
回复
FileOutputStream("文件名,true);
能不能给个小例子??
sundayfleet 2003-10-14
  • 打赏
  • 举报
回复
写入的时候用输出流 FileOutputStream("文件名",true);参数true就保证写入数据不覆盖,而是接在下面。
读数据时用BufferedInputStream流,该流有一个readLine()方法,按行读取,很好用
shloshlo 2003-10-14
  • 打赏
  • 举报
回复
基本解决了。谢谢各位!
但是写文件的时候每它只能写一行一长串,我让它分行写的时候每次都会覆盖,只剩下最后一条记录。
我如何能让它分行写呢?
shloshlo 2003-10-14
  • 打赏
  • 举报
回复
FileWriter("文件名", true)即可:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
"new.txt", true)));
out.println(result);
out.close();

问题解决,谢谢各位,马上给分!
sundayfleet 2003-10-14
  • 打赏
  • 举报
回复
import java.io.*;
public class test
{
public static void main(String []args)
{
String s = new String("hello every\n");//这里的字符串可以换
int bytes;
try
{ ByteArrayInputStream bis = new ByteArrayInputStream (s.getBytes());
FileOutputStream fos=new FileOutputStream("Target.txt",true);
while((bytes=bis.read())!=-1)
{ fos.write(bytes); }
bis.close();
fos.close();
}catch(IOException e){}
}
}
fft123 2003-10-10
  • 打赏
  • 举报
回复
假如你读取文本后,已经将其中的一行转成字符串了
如String s = "abc ef ghijk";
然后用jdk1.4的split方法,把这个字符串转成一个数组
(jdk1.3用StringTokenizer转)
String[] t1 = s.split(" ");//分隔符是空格
自己根据数组长度定义一个数组t2 (这里可以定义一些常量字符串first,second,然后根据数组长度把常量字符串往里加)
String[] t2 = new String[]{"first","second","third"};
然后拼字符串
String result = "";
for(int i=0;i<t1.length;i++)
{
result = result + t2[i] + "_" + t1[i] + ",";
}
result = result + "end.";
yangFrame 2003-10-10
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.*;

public class ReadFileTest
{
public static void main(String[] args)
{
//读入
try{
BufferedReader br = new BufferedReader(new FileReader("t.txt"));
String line;
StringBuffer buffer=new StringBuffer();

line=br.readLine();
StringTokenizer st = new StringTokenizer(line);
String[] t = new String[]{"first_",",second_",",third_"};
int i=0;
String result = "";
while(st.hasMoreTokens())
{
result=result + t[i] + st.nextToken();
i++;
}
result = result + ",end.";

/*
while((line=br.readLine())!= null)
{
if(line.indexOf("string")==-1)
{
buffer.append(line);
buffer.append("\r\n");
}
}*/
br.close();
//写文件
PrintWriter out=new PrintWriter(new FileWriter("t.txt"));
out.println(result);
out.close();
//回显屏幕
BufferedReader in=new BufferedReader(new FileReader("t.txt"));
while((line=in.readLine())!=null)
{
System.out.println(line);
}
in.close();
}
catch(Exception ex){ System.out.println("IOException error!");}
}
}
fft123 2003-10-10
  • 打赏
  • 举报
回复
文本文件的每行的格式是这样的:abc ef ghijk (各段长度未知)
用split或者StringTokenizer得到数组后
取得数组长度 //假设为t1.length
事先定义一个String数组(假设为t3),里面放从first到fifty的字符串(够长就可以了,看你的文本里存的东西最多可能有多少个空格)
然后String[] t2 = new String[t1.length];
for(int i=0;i<t2.length;i++)
{
t2[i]=t3[i];//根据数组长度,放相应长度的英文数字
}

剩下的就是把t1数组里的东西和t2数组的东西拼起来就完了
realzealy 2003-10-10
  • 打赏
  • 举报
回复
用StringTokenizer的方法
String s = "abc ef ghijk";
StringTokenizer st = new StringTokenizer(s);
String[] t = new String[]{"first","second","third"};
int i=0;
String result = "";
while(st.hasMoreTokens())
{
result=result + t[i] + st.nextToken();
i++;
}
result = result + "end.";

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧