·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » JAVA写文件

JAVA写文件

类别: JSP教程  评论数:0 总得分:0
/* Writefile.java
接收键盘的输入,并原样输出到屏幕上
此外,还有将键盘输入的数据按顺序存放到文件中
使用方法:java Writefile 文件名
要结束此程序时,请在行的开始部分输入一个#号。
*/

import java.io.*;

// Class Writefile
public class Writefile
{
public static void main(String[] args)
{
byte[] buff = new byte[1024];
boolean cont = true; // 循环控制变量
FileOutputStream outfile = null; // 文件输出对象

// 生成对象outfile,准备输出到文件
try
{
outfile = new FileOutputStream(args[0]);
}
catch (FileNotFoundException e)
{
System.err.println("文件不存在");
System.exit(1);
}

// 行首没有输入句号时执行如下循环
while (cont)
{
try
{
int n = System.in.read(buff); // 从System.in读入数据
System.out.write(buff, 0, n); // 写入到System.out中
if (buff[0]==´#´)
{
cont = false;
}
else
{
outfile.write(buff, 0, n);
}

}
catch (Exception e)
{
System.exit(1);
}
}

// 关闭文件
try
{
outfile.close();
}
catch (IOException e)
{
System.err.println("文件错误");
System.exit(1);
}
}
}
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1