当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

java的io简单应用

java的io系统是比较庞大和复杂的,一般新手对它的概念也是感觉不是很清楚,我做了几个简单的例子希望对大家有一些帮助。

//从文件里读数据,显示到屏幕上
package com.yourcompany.core;

import java.io.*;

public class Think {

public static void main(String[] args) {

try {

FileInputStream file = new FileInputStream("d:\\sss.txt");
char[] c = new char[file.available()];
for (int i = 0 ; i<c.length;i++) {
c[i] = (char)file.read();

}
System.out.print(c);

}
catch(FileNotFoundException e) {

e.printStackTrace();
}
catch(IOException e) {

e.printStackTrace();
}

}
}
//在屏幕上写东西到文件中
package com.yourcompany.core;

import java.io.*;
import java.util.*;


public class Think {

public static void main(String[] args) {

try {

byte[] c = new byte[1000] ;
System.in.read(c);
FileOutputStream file = new FileOutputStream("d:\\s.txt");
file.write(c);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
//从一个文件复制到另一个文件
package com.yourcompany.core;

import java.io.*;
import java.util.*;


public class Think {

public static void main(String[] args) {

try {

FileInputStream file = new FileInputStream("d:\\sss.txt");
FileOutputStream fileout = new FileOutputStream("d:\\aaa.txt");
byte[] c = new byte[file.available()];
for(int i = 0;i<c.length;i++) {
c[i] = (byte)file.read();
}
fileout.write(c);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
//在屏幕上你输入什么我就输入什么
package com.yourcompany.core;

import java.io.*;
import java.util.*;


public class Think {

public static void main(String[] args) {

try {

byte [] c = new byte[1000];
int cc = System.in.read(c);
for (int i=0;i<cc;i++) {
System.out.print((char)c[i]);
}
}
catch(Exception e) {
e.printStackTrace();
}

}
}



相关内容
赞助商链接