import java.io.*;
public class MyReadFile{
public static void main(String args[]){
try{
//第一步,定位数据源
File f = new File("d:/work/test.txt");
//第二步,建立管道
FileInputStream fis = new FileInputStream(f);
//第三步,操作管道
System.out.println(fis.read());
}catch(Exception e){
}
}
}
E:\work\170706>javac MyReadFile.java
E:\work\170706>java MyReadFile
97
import java.io.*;
public class MyReadFile{
public static void main(String args[]){
try{
//第一步,定位数据源
File f = new File("d:/work/test.txt");
//第二步,建立管道
FileInputStream fis = new FileInputStream(f);
//第三步,操作管道
for(int i = 0;i < fis.available(); i++){
System.out.print((char)fis.read());
}
}catch(Exception e){
}
}
}
运行结果
import java.io.*;
public class MyReadFile{
public static void main(String args[]){
try{
//第一步,定位数据源
File f = new File("d:/work/test.txt");
//第二步,建立管道
FileInputStream fis = new FileInputStream(f);
//第三步,操作管道
for(int i = 0;i < fis.available(); i++){
System.out.print(fis.available()+"==>"+(char)fis.read());
}
}catch(Exception e){
}
}
}
import java.io.*;
public class MyReadFile{
public static void main(String args[]){
try{
//第一步,定位数据源
File f = new File("d:/work/test.txt");
//第二步,建立管道
FileInputStream fis = new FileInputStream(f);
//第三步,操作管道
int length = fis.available();
for(int i = 0;i < length;i++){
System.out.print((char)fis.read());
}
}catch(Exception e){
}
}
}
import java.io.*;
public class MyReadFile{
public static void main(String args[]){
try{
//第一步,定位数据源
File inFile = new File("d:/work/test.txt");
File outFile = new File("d:/work/test1.txt");
//第二步,建立管道
FileInputStream fis = new FileInputStream(inFile);
FileOutputStream fos = new FileOutputStream(outFile);
//第三步,操作管道
int length = fis.available();
for(int i = 0;i < length;i++){
fos.write(fis.read());
}
}catch(Exception e){
}
}
}
import java.io.*;
public class MyReadFile {
public static void main(String[] args){
try {
File inFile = new File("E:/Program Files/Java/jdk1.8.0_73/src.zip");
File outFile = new File("D:/src.zip");
FileInputStream fis = new FileInputStream(inFile);
FileOutputStream fos = new FileOutputStream(outFile);
byte[] tmp = new byte[8192];
//处理大部分内容
int length = fis.available()/8192;
for (int i = 0; i < length; i++) {
fis.read(tmp);
fos.write(tmp);
}
//处理最后剩下的内容
int size = fis.read(tmp);
fos.write(tmp, 0, size);
} catch (Exception e) {
// TODO: handle exception
}
}
}