将键盘的输入写入文件,第一次输入按回车没有问题,第二次就会报数组下标越界异常
怎么回事呢?import java.io.*;public class WriteToFile
{
public static void main(String[] args) throws Exception
{
File toWrite = new File(System.getProperty("user.dir")+File.separator+"FileForWrite.txt");
FileOutputStream fos = new FileOutputStream(toWrite);
String input = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
byte[] buffer;
int length=0; while((input=in.readLine())!=null)
{
buffer = input.getBytes();
if(input.equals("quit")) break;
else
{
fos.write(buffer,length,buffer.length);
length += buffer.length;
}
}
fos.close();
}
}
怎么回事呢?import java.io.*;public class WriteToFile
{
public static void main(String[] args) throws Exception
{
File toWrite = new File(System.getProperty("user.dir")+File.separator+"FileForWrite.txt");
FileOutputStream fos = new FileOutputStream(toWrite);
String input = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
byte[] buffer;
int length=0; while((input=in.readLine())!=null)
{
buffer = input.getBytes();
if(input.equals("quit")) break;
else
{
fos.write(buffer,length,buffer.length);
length += buffer.length;
}
}
fos.close();
}
}
解决方案 »
- java_Socket_inputStrem
- 新手自学JAVA,求教叫有效方法,最好能事半功倍(时间不多了啊)
- java 使用BigDecimal 类求一个数学概率, 抛出异常
- JFrame 进程优先级
- java连接数据库sql
- 在线等答案 急!各位帮忙看看
- 求教:Collection、Vector、Set、Map等各种集合类的特点和区别
- 如何把Object型的数组转换为String型的数组
- 为什么执行不了
- 关于执行project文件
- 请问在java中可以调用用C++写的函数或程序吗吗…………………………!谢谢了
- 正则表达式的递归问题 Recursive Regular Expressions
改为:
fos.write(buffer,0,buffer.length);这里的偏移量是相对与buffer的