下面是我做的一个方法,可为什么老抛出异常,并且不能加到文本文件末尾??
到底这个方法错在哪?
static void addindic(String s){
String s1=null;
try{FileInputStream rf1=new FileInputStream("dictionary.txt");
int n=512;
byte buffer[]=new byte[n];
while((rf1.read(buffer,0,n)!=-1)&&(n>0))
{s1=new String(buffer);
rf1.close();
}
PrintWriter a=new PrintWriter(new FileOutputStream("dictionary.txt"));
a.write(s,s1.length(),s.length());
} catch(Exception e)
{System.out.println(e);}
}
解决方案 »
- 想用java做一个FTP客户端,但是没有思路
- 对WORD文档进行精细化操作,使用什么方式比较好?
- 大家帮我看看啊,一天都没有解决问题
- 如何将Map集合转换成二维数组?
- 局域网上如何查找服务器的ip地址啊?
- 什么时候该用TYPE_SCROLL_INSENSITIVE 什么时候该用TYPE_SCROLL_SENSITIVE
- 答案是A,有点不明白,
- 令人头痛的问题:如何判断数据库表中的字段已全部遍历
- java 与单片机通信~help~~!
- 一个WEB Server的配置问题?
- <script language="javascript" src="/tree/mztreeview10.js"></script>这句话是什么意思啊!
- 写java程序时..怎么用相对路径啊?
out.println("add");
void println(String x) 打印 String,然后终止该行。
PrintWriter pw = new PrintWriter(new FileWriter("ok.txt",true),true);
pw.append(s);//FileWriter和FileOutputStream第二個參數要true.沒有true的話會打開一個空文件。
boolean append)
throws FileNotFoundException创建一个向指定 File 对象表示的文件中写入数据的文件输出流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。创建一个新 FileDescriptor 对象来表示此文件连接。
首先,如果有安全管理器,则用 file 参数表示的路径作为参数来调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。
/**
* @version 0.01
* @author zhanshengkui
* 目标
*/
import java.io.*;
public class TestKeyIn { /**
* @param args
*/
//读取键盘的一行数据
//写入文件;
public static void readLineFile(String toFile){
//带缓存的字符流
//不可直接使用
//System.in表示标准键盘输入
BufferedReader buff = new BufferedReader(
new InputStreamReader(System.in));
RandomAccessFile ra = null;
try{
ra = new RandomAccessFile(toFile,"rw");
//
ra.seek(ra.length());//移动文件读写指针
String line = buff.readLine();
while(!line.equalsIgnoreCase("quit")){
//ra.writeUTF(line);
ra.write(line.getBytes("GBK"));
ra.write("\r\n".getBytes("GBK"));
line = buff.readLine();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException el){
el.printStackTrace();
}finally{
try{
buff.close();
ra.close();
}catch(FileNotFoundException eo){
eo.printStackTrace();
}catch(IOException ei){
ei.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
readLineFile("c://a.txt");
}}
------------------
jk88811(jkzqw) ( ) 信誉:100 Blog 2006-11-25 22:38:46 得分: 0
API文档上讲得清清楚楚啦...public FileOutputStream(File file,
boolean append)
throws FileNotFoundException创建一个向指定 File 对象表示的文件中写入数据的文件输出流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。创建一个新 FileDescriptor 对象来表示此文件连接。
首先,如果有安全管理器,则用 file 参数表示的路径作为参数来调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。
File file = new File("***");
RandomAccessFile raf = new RandomAccessFile (file);
raf.seek(file.length);
//再把字符串写入
.....