package TTTT;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
*IO文件字节输入流标准步骤:
* 1.创建源
* 2.创建流
* 3.操作
* 4.释放资源
* @author 佳哥
*
*/
public class IO {
public static void main(String[] args) {
//创建源----------1
File file = new File("IOtest.txt");//使用相对路径
//选择流----------2
InputStream is = null;
try {
is = new FileInputStream(file);
//操作-------3
int length = 0;//接受获取数组的长度
//创建数组
byte[] array = new byte[2];
while((length = is.read(array)) != -1)//文本中没有数据读入会是-1
{
String str = new String(array,0,length);
System.out.print(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //此处会有异常需要throws或者try-catch
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(is != null)//避免出现空指针
{
//.释放资源---------------4
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Java文件字节输入流
最新推荐文章于 2024-07-10 21:37:49 发布