JAVA IO字节流
目录[ Hide ]
IO流概述及其分类
FileInputStream
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo1_FileInputStream {
public static void main(String[] args) throws IOException {
// demo1();
FileInputStream fis = new FileInputStream("xxx.txt");
int b;
while((b = fis.read()) != -1){
System.out.println(b);
}
fis.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
int x = fis.read();
System.out.println(x);
int y = fis.read();
System.out.println(y);
int z = fis.read();
System.out.println(z);
int a = fis.read();
System.out.println(a); //文件的结束标记是-1
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo1_FileInputStream {
public static void main(String[] args) throws IOException {
// demo1();
FileInputStream fis = new FileInputStream("xxx.txt");
int b;
while((b = fis.read()) != -1){
System.out.println(b);
}
fis.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
int x = fis.read();
System.out.println(x);
int y = fis.read();
System.out.println(y);
int z = fis.read();
System.out.println(z);
int a = fis.read();
System.out.println(a); //文件的结束标记是-1
fis.close();
}
}
read()方法返回值为什么是int
FileOutputStream
package com.heima.stream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2_FileOutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("yyy.txt"); //创建字节输出流对象如果没有就自动创建一个
fos.write(96); //虽然写的是一个int数,但是到文件上的是一个字节,会自动去除前三个八位
fos.write(96);
fos.write(96);
}
}
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2_FileOutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("yyy.txt"); //创建字节输出流对象如果没有就自动创建一个
fos.write(96); //虽然写的是一个int数,但是到文件上的是一个字节,会自动去除前三个八位
fos.write(96);
fos.write(96);
}
}
FileOutputStream追加
package com.heima.stream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2_FileOutputStream {
//FileOutStream在创建对象的时候如果没有这个文件会帮我们创建出来,如果有这个文件,就会先将文件清空,然后将内容覆盖
public static void main(String[] args) throws IOException {
// demo1();
//如果想追加就在第二个参数传true
FileOutputStream fos = new FileOutputStream("yyy.txt",true);
fos.write(99);
fos.write(99);
}
private static void demo1() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("yyy.txt"); //创建字节输出流对象如果没有就自动创建一个
fos.write(96); //虽然写的是一个int数,但是到文件上的是一个字节,会自动去除前三个八位
fos.write(96);
fos.write(96);
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2_FileOutputStream {
//FileOutStream在创建对象的时候如果没有这个文件会帮我们创建出来,如果有这个文件,就会先将文件清空,然后将内容覆盖
public static void main(String[] args) throws IOException {
// demo1();
//如果想追加就在第二个参数传true
FileOutputStream fos = new FileOutputStream("yyy.txt",true);
fos.write(99);
fos.write(99);
}
private static void demo1() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("yyy.txt"); //创建字节输出流对象如果没有就自动创建一个
fos.write(96); //虽然写的是一个int数,但是到文件上的是一个字节,会自动去除前三个八位
fos.write(96);
fos.write(96);
}
}
拷贝图片
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("jpg.jpg");//创建输入流对象,关联jpg.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg");//创建输出流对象,关联copy.jpg
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
fis.close();
fos.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("jpg.jpg");//创建输入流对象,关联jpg.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg");//创建输出流对象,关联copy.jpg
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
fis.close();
fos.close();
}
}
拷贝音频文件画原理图
一个字节一个字节复制粘贴
字节数组拷贝之available()方法
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
//第二种方式不推荐使用,有可能导致内存溢出
FileInputStream fis = new FileInputStream("jpg.jpg");
FileOutputStream fos = new FileOutputStream("copy.jpg");
// int len = fis.available();
//
// System.out.println(len);
byte[] arr = new byte[fis.available()];
fis.read(arr); //将文件上的字节读取到内存
fos.write(arr); //将内存的数组写到新文件
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("jpg.jpg");//创建输入流对象,关联jpg.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg");//创建输出流对象,关联copy.jpg
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
fis.close();
fos.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
//第二种方式不推荐使用,有可能导致内存溢出
FileInputStream fis = new FileInputStream("jpg.jpg");
FileOutputStream fos = new FileOutputStream("copy.jpg");
// int len = fis.available();
//
// System.out.println(len);
byte[] arr = new byte[fis.available()];
fis.read(arr); //将文件上的字节读取到内存
fos.write(arr); //将内存的数组写到新文件
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("jpg.jpg");//创建输入流对象,关联jpg.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg");//创建输出流对象,关联copy.jpg
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
fis.close();
fos.close();
}
}
定义小数组
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4_ArrayCopy {
/*
*
* 第三种拷贝
* 定义小数组
* */
public static void main(String[] args) throws IOException {
// demo1();
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[2];
int len;
while((len = fis.read(arr))!= -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[2];
int a = fis.read(arr);//将文件上的字节读到字节数组中
System.out.println(a);//读到的有效字节个数
for (byte b : arr) { //第一次获取到文件上的a和b
System.out.println(b);
}
System.out.println("--------------------");
int d = fis.read(arr);
System.out.println(d);
int b = fis.read(arr);
for (byte c : arr) {
System.out.println(c);
}
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4_ArrayCopy {
/*
*
* 第三种拷贝
* 定义小数组
* */
public static void main(String[] args) throws IOException {
// demo1();
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[2];
int len;
while((len = fis.read(arr))!= -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[2];
int a = fis.read(arr);//将文件上的字节读到字节数组中
System.out.println(a);//读到的有效字节个数
for (byte b : arr) { //第一次获取到文件上的a和b
System.out.println(b);
}
System.out.println("--------------------");
int d = fis.read(arr);
System.out.println(d);
int b = fis.read(arr);
for (byte c : arr) {
System.out.println(c);
}
fis.close();
}
}
定义小数组的标准格式
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4_ArrayCopy {
/*
*
* 第三种拷贝
* 定义小数组
* */
public static void main(String[] args) throws IOException {
// demo1();
// demo2();
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[1024];
int len;
while((len = fis.read(arr))!= -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void demo2() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[2];
int len;
while((len = fis.read(arr))!= -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[2];
int a = fis.read(arr);//将文件上的字节读到字节数组中
System.out.println(a);//读到的有效字节个数
for (byte b : arr) { //第一次获取到文件上的a和b
System.out.println(b);
}
System.out.println("--------------------");
int d = fis.read(arr);
System.out.println(d);
int b = fis.read(arr);
for (byte c : arr) {
System.out.println(c);
}
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4_ArrayCopy {
/*
*
* 第三种拷贝
* 定义小数组
* */
public static void main(String[] args) throws IOException {
// demo1();
// demo2();
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[1024];
int len;
while((len = fis.read(arr))!= -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void demo2() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[2];
int len;
while((len = fis.read(arr))!= -1){
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[2];
int a = fis.read(arr);//将文件上的字节读到字节数组中
System.out.println(a);//读到的有效字节个数
for (byte b : arr) { //第一次获取到文件上的a和b
System.out.println(b);
}
System.out.println("--------------------");
int d = fis.read(arr);
System.out.println(d);
int b = fis.read(arr);
for (byte c : arr) {
System.out.println(c);
}
fis.close();
}
}
BufferedInputStream和BufferOutputStream拷贝
package com.heima.stream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5_BufferCopy {
//缓冲区copy
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("xxx.txt");//创建输入流对象,关联xxx.txt
FileOutputStream fos = new FileOutputStream("copy.txt");//创建输出流对象,关联copy.txt
BufferedInputStream bis = new BufferedInputStream(fis);//创建缓冲区对象,对输入流进行包装让其变得更加强大
BufferedOutputStream bos = new BufferedOutputStream(fos);//创建缓冲区输出流对象,对输出流进行包装
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5_BufferCopy {
//缓冲区copy
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("xxx.txt");//创建输入流对象,关联xxx.txt
FileOutputStream fos = new FileOutputStream("copy.txt");//创建输出流对象,关联copy.txt
BufferedInputStream bis = new BufferedInputStream(fis);//创建缓冲区对象,对输入流进行包装让其变得更加强大
BufferedOutputStream bos = new BufferedOutputStream(fos);//创建缓冲区输出流对象,对输出流进行包装
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
}
flush和close方法的区别
package com.heima.stream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5_BufferCopy {
//缓冲区copy
public static void main(String[] args) throws IOException {
// demo1();
//flush和close的区别
//close方法具备刷新的功能,在关闭流之前,就会先刷新一次缓冲区,将缓冲区的字节全都刷新到文件上再关闭,close刷完不能写了
//flush具备刷新功能,刷完之后还可以继续写
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("xxx.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.txt"));
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");//创建输入流对象,关联xxx.txt
FileOutputStream fos = new FileOutputStream("copy.txt");//创建输出流对象,关联copy.txt
BufferedInputStream bis = new BufferedInputStream(fis);//创建缓冲区对象,对输入流进行包装让其变得更加强大
BufferedOutputStream bos = new BufferedOutputStream(fos);//创建缓冲区输出流对象,对输出流进行包装
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5_BufferCopy {
//缓冲区copy
public static void main(String[] args) throws IOException {
// demo1();
//flush和close的区别
//close方法具备刷新的功能,在关闭流之前,就会先刷新一次缓冲区,将缓冲区的字节全都刷新到文件上再关闭,close刷完不能写了
//flush具备刷新功能,刷完之后还可以继续写
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("xxx.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.txt"));
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");//创建输入流对象,关联xxx.txt
FileOutputStream fos = new FileOutputStream("copy.txt");//创建输出流对象,关联copy.txt
BufferedInputStream bis = new BufferedInputStream(fis);//创建缓冲区对象,对输入流进行包装让其变得更加强大
BufferedOutputStream bos = new BufferedOutputStream(fos);//创建缓冲区输出流对象,对输出流进行包装
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
}
字节流读写中文
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo6_Chinese {
public static void main(String[] args) throws IOException {
// demo1();
FileOutputStream fos = new FileOutputStream("copy.txt");
fos.write("你好啊老王".getBytes());
fos.write("\r\n".getBytes());
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[4];
int len;
while((len = fis.read(arr)) != -1){
System.out.println(new String(arr,0,len));
}
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo6_Chinese {
public static void main(String[] args) throws IOException {
// demo1();
FileOutputStream fos = new FileOutputStream("copy.txt");
fos.write("你好啊老王".getBytes());
fos.write("\r\n".getBytes());
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
byte[] arr = new byte[4];
int len;
while((len = fis.read(arr)) != -1){
System.out.println(new String(arr,0,len));
}
fis.close();
}
}
流的标准处理异常代码1.6版本及其以前
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7_TryFinally {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("copy.txt");
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
}finally{
try{
if(fis != null)
fis.close();
}finally{ //try finally嵌套 目的是能关一个尽量关一个
if(fos != null)
fos.close();
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7_TryFinally {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("copy.txt");
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
}finally{
try{
if(fis != null)
fis.close();
}finally{ //try finally嵌套 目的是能关一个尽量关一个
if(fos != null)
fos.close();
}
}
}
}
流的标准处理异常代码1.7版本
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7_TryFinally {
public static void main(String[] args) throws IOException {
// demo1();
try(
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
MyClose mc = new MyClose();
){
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
}
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("copy.txt");
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
}finally{
try{
if(fis != null)
fis.close();
}finally{ //try finally嵌套 目的是能关一个尽量关一个
if(fos != null)
fos.close();
}
}
}
}
class MyClose implements AutoCloseable{
public void close(){
System.out.println("关了吗");
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7_TryFinally {
public static void main(String[] args) throws IOException {
// demo1();
try(
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
MyClose mc = new MyClose();
){
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
}
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("copy.txt");
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
}finally{
try{
if(fis != null)
fis.close();
}finally{ //try finally嵌套 目的是能关一个尽量关一个
if(fos != null)
fos.close();
}
}
}
}
class MyClose implements AutoCloseable{
public void close(){
System.out.println("关了吗");
}
}
图片加密
package com.heima.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy1.jpg"));
int b;
while((b = bis.read())!=-1){
bos.write(b ^ 123);
}
bis.close();
bos.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy1.jpg"));
int b;
while((b = bis.read())!=-1){
bos.write(b ^ 123);
}
bis.close();
bos.close();
}
}
拷贝文件
package com.heima.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test2 {
/*
* 在控制台录入文件路径,将文件拷贝到当前项目下
*
* 分析
*
* 1.创建键盘录入对象
* 2.定义方法对键盘录入的路径进行判断,如果是文件就返回
* 3.在主方法中接收该文件
* 4.读和写该文件
* */
public static void main(String[] args) throws IOException {
File file = getFile();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
//定义方法,并封装成file对象返回
/*
* 1.返回值类型File
* 2.参数列表无
*
* */
public static File getFile(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件的路径");
while(true){
String line = sc.nextLine(); //接收键盘录入的路径
File file = new File(line); //封装成File对象并对其进行判断
if(!file.exists()){
System.out.println("您录入的文件不存在,请重新输入:");
}else if(file.isDirectory()){
System.out.println("您录入的是文件夹路径,请重新输入:");
}else{
return file;
}
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test2 {
/*
* 在控制台录入文件路径,将文件拷贝到当前项目下
*
* 分析
*
* 1.创建键盘录入对象
* 2.定义方法对键盘录入的路径进行判断,如果是文件就返回
* 3.在主方法中接收该文件
* 4.读和写该文件
* */
public static void main(String[] args) throws IOException {
File file = getFile();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
//定义方法,并封装成file对象返回
/*
* 1.返回值类型File
* 2.参数列表无
*
* */
public static File getFile(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件的路径");
while(true){
String line = sc.nextLine(); //接收键盘录入的路径
File file = new File(line); //封装成File对象并对其进行判断
if(!file.exists()){
System.out.println("您录入的文件不存在,请重新输入:");
}else if(file.isDirectory()){
System.out.println("您录入的是文件夹路径,请重新输入:");
}else{
return file;
}
}
}
}
录入数据拷贝到文件
package com.heima.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test3 {
//将键盘录入的数据copy到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
/*
* 分析:
* 1.创建键盘录入对象
* 2.创建输出流对象,关联test.txt文件
* 3.定义无限循环,遇到quit退出循环
* 4.如果不是quit,就将内容写出
* 5.关闭流
* */
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据:");
FileOutputStream fos = new FileOutputStream("xxx.txt");
while(true){
String line = sc.nextLine();
if("quit".equals(line)){
break;
}
fos.write(line.getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test3 {
//将键盘录入的数据copy到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
/*
* 分析:
* 1.创建键盘录入对象
* 2.创建输出流对象,关联test.txt文件
* 3.定义无限循环,遇到quit退出循环
* 4.如果不是quit,就将内容写出
* 5.关闭流
* */
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据:");
FileOutputStream fos = new FileOutputStream("xxx.txt");
while(true){
String line = sc.nextLine();
if("quit".equals(line)){
break;
}
fos.write(line.getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
}
}