java.io.FileInputStream文件字节输入流,任何类型的文件都可以采用这个流来读,以字节的方式完成输入的操作,读的操作(硬盘-》内存)
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream file = null;
byte[] bytes = new byte[100];
// 创建文件字节流对象
try {
file = new FileInputStream("C:\\Users\\xjj eyujun\\Desktop\\个人小空间.txt");
int i = 0;
while ((i = file.read(bytes)) != -1) {
System.out.println(new String(bytes ,0, i));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamTest {
public static void main(String[] args) {
FileOutputStream file = null;
byte[] bytes = {99,127,123};
String s = "我是中国人";
byte[] bs = s.getBytes();
try {
// file = new FileOutputStream("stream.txt");
// 追加的方式
file = new FileOutputStream("stream.txt",true);
file.write(bytes);
file.write(bs);
file.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (file != null){
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件复制:import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
FileInputStream file1 = null;
FileOutputStream file2 = null;
byte[] bytes = new byte[1024 * 1024];
try {
// 创建输入流对象
file1 = new FileInputStream("stream.txt");
// 创建输出流对象
file2 = new FileOutputStream("d:\\new_stream.txt");
int i = 0;
while ((i = file1.read(bytes)) != -1) {
file2.write(bytes, 0, i);
}
file2.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file1 != null) {
try {
file1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file2 != null) {
try {
file2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件字符输入流:import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
// 文件字符输入流 只能读取普通文本
public class FileReaderTest {
public static void main(String[] args) {
FileReader file = null;
try {
// 创建文件字符输入流
file = new FileReader("stream.txt");
char[] chars = new char[4];//一次读取4个字符
int i = 0;
while ((i = file.read(chars)) != -1) {
System.out.println(new String(chars, 0, i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件字符输出流:import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
FileWriter file = null;
try {
// 创建文件字符输出流对象
file = new FileWriter("new_stream.txt");
char[] chars = {'我','叫','T','M','D','!'};
file.write(chars);
// 第2个字符开始,写4个字符
file.write(chars,2,4);
file.write("我叫SDIOANHIDS");
// 刷新
file.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (file!=null){
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
声明:1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!