1IO流
1IO流是什么:流是一种抽象概念,它代表了数据的无结构化传递。按照流的方式进行输入输出,数据被当成无结构的字节序列或字符序列。
2IO流能干什么:可以进行在本地磁盘和网络进行文件输入输出
3IO流分类:1输入,输出流
2:字符,字节流
字符流包括reader,writer。
字节流包括InputStream,OutputStream
4字符流读写文件
1字符输入流
import java.io.FileReader;
import java.io.Reader;
import java.io.IOException;
public class AsSingleCharacter {
public static void main(String[] args) throws IOException {
Reader reader = new FileReader("D:\\JAVA\\IDEA\\file\\helloworld");
int number;
while ((number = reader.read()) != -1) {
System.out.println(number);
System.out.println((char)number);
}
reader.close();
}
}
2字符输出流
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriteData1 {
public static void main(String[] args) throws IOException {
Writer writer = new FileWriter("D:\JAVA\IDEA\file\helloworld2");
writer.write('我');//一个一个写入
writer.write('是');
writer.write('谁');
writer.write('?');
char[] c1 = {'1','2','3','4','5','6'}
char[] c2 = {'6','5','4','3','2','1'};
writer.write(c1, 0, 6);//分别输入字符数组名,起始位置,长度
writer.write(c2);//整个写入
writer.write("压缩yyds");以字符串形式写入
writer.close();
}
}
3字节输入输出流(与字符输入输出流类似所以写在一起)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("D:/JAVA/IDEA/helloworld/0.png");
OutputStream outputStream = new FileOutputStream("D:/JAVA/IDEA/a/1.png");
byte[] byte = new byte[1024];
int length;
StringBuffer stringBuffer =new StringBuffer();
while ((length = inputStream.read(byte)) != -1)
{
outputStream.write(byte, 0, length);
stringBuffer.append(new String(byte));
}
System.out.println(stringBuffer);
inputStream.close();
outputStream.close();
}
}
2File类
1File是什么
File表示了某个磁盘上文件或者文件夹
2File构造方法
import java.io.File;
import java.io.IOException;
public class File {
public static void main (String[] args) throws IOException
File (String pathname):
File file1 = new File("D:\\JAVA\\IDEA\\helloword");//直接用路径初始化
System.out.println("file1);
File(String s, String son):
File file2 = new File("D:/JAVA/IDEA/", "helloworld");
System.out.println("file2);//分为父子路径后初始化
File(File s, String son):
File s= new File("D:/JAVA/IDEA");
File file3 = new File(s, "helloworld");
System.out.println("file3);//与上面类似,不过将父路径封装为File类型
}
}
3File成员方法
1构造成员方法与删除:
import java.io.File;
import java.io.IOException;
public class Create {
public static void main(String[] args) throws IOException{
File file1 = new File("D:\\JAVA\\IDEA\\helloworld\\666.txt");
boolean b1 = file1.createNewFile();
boolean b2 = file1.createNewFile();
System.out.println("b1);
System.out.println("b2);//不能创建两次所以为false
boolean b3=file1.delete();
System.out.println("b3");//目录删除
boolean b4 = file1.createNewFile();
System.out.println("b4");//删除后创建所以为true
File file2 = new File("D:\\JAVA\\IDEA\\helloworld\\777.txt");
boolean b5 = file2.mkdir();
System.out.println(bool2);//单级目录成功创建
File file3 = new File("D:\\JAVA\\IDEA\\helloworld\\888\\1\\2\\3\\4");
boolean b6 = file3.mkdirs();
System.out.println(b6);//多级目录成功创建
}
}
2其他常用方法
isDirectory();
isFile();
exists;
getAbsolutePath();
getPath();
getName();
getlength();
getParent();
list();
listFiles();