package FileOp;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import javax.swing.JOptionPane;public class FileDemo {
public static void main(String[] args) {
///////读文件
File file = new File("e:/a.txt");
FileInputStream inputfile = null;
StringBuffer str= new StringBuffer(" ");
try{
inputfile = new FileInputStream(file);
} catch(FileNotFoundException fe) {
fe.printStackTrace();
}
int readbytes;
try{
while((readbytes = inputfile.read())!=-1){
str.append((char)readbytes);
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.print(str);
try{
inputfile.close();
}catch(IOException e) {
e.printStackTrace();
}
///写文件
File fOut = new File("e:/b.txt");
FileOutputStream outStream=null;
try{
outStream = new FileOutputStream(fOut);
}catch(FileNotFoundException fe) {
fe.printStackTrace();
}
try{
outStream.write("abcdefg".getBytes());
}catch(IOException ioe) {
ioe.printStackTrace();
}
try{
outStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
解决方案 »
- 求助高手关于start javaw -jar example.jar启动的bat后,由于这个程序是阻塞的,会一直执行下去,如何做一个终止执行的bat呢
- import中出现问题
- nio 能支持tcp 5000个client么??
- 用jdbc怎么删除想关联的主从表
- 求助:菜单设置问题
- 怎么样打印出,hashtable中的所有内容啊?感谢
- 在linux下安装了jre后,为什么浏览器仍然不能显示applet
- 请大虾帮我看看这段AWT程序,为什么运行的时候只生成了一个没有名字的白窗口
- 问一个比较弱的问题,怎么将字符串写入到本地文件里面。必高分重谢
- 急!急!急!面试时遇到几个算法题,求解
- HashMap中的值是固定的,但为什么每次循环遍历的结果与前次不一样呢?
- 关于在java 项目中使用代码将自己打包
package FileOp;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;import javax.swing.JOptionPane;public class FileDemo {
public static void main(String[] args) {
///////读文件
File file = new File("e:/a.txt");
FileInputStream inputfile = null;
StringBuffer str= new StringBuffer(" ");
try{
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead=0;
char content[] = new char[size];
while(readIn.ready())
charsRead +=readIn.read(content, charsRead, size-charsRead);
readIn.close();
System.out.print(new String(content, 0, charsRead));
try{
readIn.close();
}catch(IOException e) {
e.printStackTrace();
}
}catch(IOException exx) {
exx.printStackTrace();
}
///写文件
File fOut = new File("e:/b.txt");
FileOutputStream outStream=null;
try{
outStream = new FileOutputStream(fOut);
}catch(FileNotFoundException fe) {
fe.printStackTrace();
}
try{
outStream.write("abcdefg".getBytes());
}catch(IOException ioe) {
ioe.printStackTrace();
}
try{
outStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}