java的io简单应用 选择自 FrankTaylor 的 Blog
2008-01-05 18:36:53 来源:WEB开发网//从文件里读数据,显示到屏幕上
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();
}
}
}
更多精彩
赞助商链接