如何 PrintWriter 在文件的指定位置写数据 2楼的,RandomAccessFile不行???????? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用RandomAccessFile吧可以的! import java.io.*;public class TestIO{ public static void main(String [] args) { try{ File file = new File("data.txt"); if (!file.exists()) { file.createNewFile(); } FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); StringBuffer buf = new StringBuffer(br.readLine()); br.readLine(); String temp; while ((temp=br.readLine())!= null) { buf = buf.append( System.getProperty("line.separator")); buf = buf.append(temp); } br.close(); FileOutputStream fos = new FileOutputStream(file); PrintWriter pw = new PrintWriter(fos); pw.write(buf.toString().toCharArray()); pw.flush(); pw.close(); }catch (IOException e) { System.out.println(e); } }}按你的方法,我做得很笨的!我写的不好,因为你如果不说用PrintWriter的话,我想会有好多办法的!今天写这个程序,可能有很大一部分是为了得分吧!不过这处程序没问题的,我都试过,前提是你要在这个class文件的同一目录中把你的data.txt文件加入,并写上数据! 首先谢谢大家回复楼上的 hillMover 谢谢你写了这么多,我调试了一下你写的这段程序:结果是将第二行的内容删除,PrintWriter 看来不是当前最灵活的选择,其实我也就是想灵活实现对.txt文件内容的修改,如果 RandomAccessFile 能灵活实现的话,那是最好了,我会加分的... 现在我用“RandomAccessFile ”改成这样用:public static void main(String [] args){File saveFilePath = new File("test.txt");try{RandomAccessFile saveFile = new RandomAccessFile(saveFilePath,"rw");String con = new String("JAVA test!");byte [] a = con.getBytes();saveFile.seek(saveFile.length());saveFile.write(a);saveFile.close();}catch(Exception e){e.printStackTrace();}}但是只能对某个位置进行修改,RandomAccessFile 有没有行的概念?应该怎么写,大伙支持一下... 把所有的数据用"\n"分开到数组str,那str[1]就是第二行数据了 最后用 hillMover(老根) 的方法可以控制行数了,谢谢" hillMover(老根)",谢谢各位... 求教java Applet简单的输入输出问题 用SWT写个程序,怎么使点击关闭时转入右下脚状态栏继续运行??? printstream问题 大家看看解决这个错误该如何分析,谢谢.... 有关hibernate的join问题,请哪位高手指点一下 一个简单问题!!! 放假散分!!!!!! 《精通EJB》(第二版) 想来北京的朋友请进! 这个问题好难啊!关于JFrame中添加组件问题。 调试问题Code attribute is absent in method that is not abstract or native 在java中如何用RSA算法实现对称密钥的传递,急
可以的!
public class TestIO
{
public static void main(String [] args)
{
try{
File file = new File("data.txt");
if (!file.exists())
{
file.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer(br.readLine());
br.readLine();
String temp;
while ((temp=br.readLine())!= null)
{
buf = buf.append( System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
}catch (IOException e)
{
System.out.println(e);
}
}
}
按你的方法,我做得很笨的!
我写的不好,
因为你如果不说用PrintWriter的话,
我想会有好多办法的!
今天写这个程序,
可能有很大一部分是为了得分吧!
不过这处程序没问题的,
我都试过,前提是你要在这个class文件的同一目录中把你的data.txt文件加入,并写上数据!
结果是将第二行的内容删除,PrintWriter 看来不是当前最灵活的选择,其实我也就是想灵活实现对.txt文件内容的修改,如果 RandomAccessFile 能灵活实现的话,那是最好了,我会加分的...
public static void main(String [] args)
{
File saveFilePath = new File("test.txt");
try{RandomAccessFile saveFile = new RandomAccessFile(saveFilePath,"rw");
String con = new String("JAVA test!");
byte [] a = con.getBytes();
saveFile.seek(saveFile.length());
saveFile.write(a);
saveFile.close();
}
catch(Exception e)
{e.printStackTrace();
}
}
但是只能对某个位置进行修改,RandomAccessFile 有没有行的概念?应该怎么写,大伙支持一下...