文件
什么是文件?
文件一般存储在哪里?
Java程序如何访问文件属性
- 从磁盘中读取文件是以页为单位的,单位大小是可能4kb,也可能是4kb的N倍,电脑配置不一样大小不一样,但是都是以页为单位的,并不是说你想读取多大字节就读多大的字节,这样效率太低了。
- 磁盘预读(预读的长度一般为页(page)的整数倍):页是存储器的逻辑块,操作系统往往将主存和磁盘存储区分割为连续的大小相等的块,每个存储块称为一页(在许多操作系统中,一页的大小为4kb)。主存和磁盘以页为单位来交换数据
- 局部性原理:发现程序和数据都有聚集成群的倾向,在一个时间段内,仅使用其中的一小部分称空间局部性;或者最近心访问过的程序代码和数据,很快又被访问的可能性很大称时间局部性
File使用
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File file = new File("abc.txt");
System.out.println(file.canExecute());
System.out.println(file.canRead());
System.out.println(file.canWrite());
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(file.exists());
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
System.out.println(file.getAbsoluteFile());
System.out.println(File.separator);
System.out.println(file.getParent());
File file1 = new File("c:/ccc.txt");
System.out.println(file1.getAbsolutePath());
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
File file2 = new File("c:/");
String[] list = file2.list();
for (String s : list) {
System.out.println(s);
}
File[] files = file2.listFiles();
for (File file3 : files) {
System.out.println(file3);
}
File file3 = new File("c:/a");
file3.mkdir();
File file4 = new File("c:/a");
file4.mkdirs();
println(new File("c:/"));
}
public static void println(File file){
if (file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
println(file1);
}
}else {
System.out.println("单个文件直接打印");
System.out.println(file.getAbsolutePath());
}
}
}
流的基本概念
- 如何读写文件?
- 通过流来读写文件
流是指一连串流动的字符,先进先出的方式 - 送信息的通过
数据源:inputStream ->目的地:outputStream
流的分类
InputStream输入字节流
FileInputSteam
public class InputStreamDemo {
public static void main(String[] args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("abc.txt");
int length = 0;
byte[] buffer = new byte[1024];
while((length = inputStream.read(buffer)) != -1){
System.out.println(new String(buffer,5,length));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ByteArrayInputStream
public class ByteArrayInputStreamDemo {
public static void main(String[] args) {
String s = "byte";
byte[] bytes = s.getBytes();
ByteArrayInputStream byteOutputStream = new ByteArrayInputStream(bytes);
int read = 0;
while((read = byteOutputStream.read())!=-1){
byteOutputStream.skip(2);
System.out.println((char) read);
}
try {
byteOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectInputStream
public class Person implements Serializable {
long serialVersionUID = 1L;
private int id;
private String name;
transient private String pwd;
public Person(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
public class ObjectInputStreamDemo {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try {
fileInputStream = new FileInputStream("123.txt");
objectInputStream = new ObjectInputStream(fileInputStream);
Object o = objectInputStream.readObject();
if (o instanceof Person){
Person person =(Person) o;
System.out.println(person);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
objectInputStream.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
DataInputStream
DataInputStream:数据字节流 可以输入各种类型的字节流
public class DataInputStreamDemo {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
fileOutputStream = new FileOutputStream("bb.txt");
fileInputStream = new FileInputStream("bb.txt");
dataOutputStream = new DataOutputStream(fileOutputStream);
dataInputStream = new DataInputStream(fileInputStream);
dataOutputStream.writeUTF("cc");
dataOutputStream.writeBoolean(true);
dataOutputStream.writeInt(11);
System.out.println(dataInputStream.readUTF());
System.out.println(dataInputStream.readBoolean());
System.out.println(dataInputStream.readInt());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dataInputStream.close();
dataOutputStream.close();
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedInputStream
public class BufferInputStreamDemo {
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("aaa.txt"));
byte[] buffer = new byte[1024];
int length = 0;
while ((length = bufferedInputStream.read(buffer))!= -1){
System.out.println(new String(buffer,0,length));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputStream字节输入流
FileOutputStream
public class OutputStreamDemo {
public static void main(String[] args) {
File file = new File("aaa.txt");
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(99);
outputStream.write("\r\nmashibing".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ByteArrayOutputStream
public class ByteArrayInputStreamDemo {
public static void main(String[] args) {
String str = "ccc";
byte[] buffer = str.getBytes();
ByteArrayInputStream byteArrayInputStream = null;
byteArrayInputStream = new ByteArrayInputStream(buffer);
int read = 0;
while((read = byteArrayInputStream.read())!=-1){
byteArrayInputStream.skip(4);
System.out.println((char)read);
}
try {
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedOutputStream
public class BufferedOutputStreamDemo {
public static void main(String[] args) {
File file = new File("123.txt");
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream =null;
try {
fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write(98);
bufferedOutputStream.write("www.baidu.com".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectOutputStream
public class ObjectOutputStreamDemo {
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
fileOutputStream = new FileOutputStream("abc.txt");
objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeUTF("ccc");
objectOutputStream.writeObject(new Person(1,"zhangsan","123456"));
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
DataArrayOutputStream
PrintSream
public class PrintStreamDemo {
public static void main(String[] args) {
PrintStream printStream = new PrintStream(System.out);
printStream.print("dfd");
printStream.write(11);
try {
printStream.write("asa".getBytes());
printStream.println();
printStream.printf("%s--%d--%.2f","aaa",123,111.111);
} catch (IOException e) {
e.printStackTrace();
}finally {
}
printStream.close();
}
}
Reader输入字符流
FileReader
public class ReaderDemo3 {
public static void main(String[] args) {
Reader reader = null;
try {
reader = new FileReader("abc.txt");
int length = 0;
char[] chars = new char[1024];
while((length = reader.read(chars))!=-1){
System.out.println(new String(chars,0,length));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
CharArrayReader
public class CharArrayReaderTest {
public static void main(String[] args) {
char[] chars = "cc".toCharArray();
CharArrayReader charArrayReader = new CharArrayReader(chars);
try {
int read = 0;
while((read = charArrayReader.read())!=-1){
System.out.println((char)read);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
charArrayReader.close();
}
}
}
BufferedReader
public class BufferedReaderTest {
public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("aaa.txt"));
String read = null;
while((read = reader.readLine())!=null){
System.out.println(read);
};
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Writer输出字符流
FileWriter
public class WriterDemo {
public static void main(String[] args) {
File file = new File("writer.txt");
Writer writer = null;
try {
writer = new FileWriter(file);
writer.write("www.ccc.com");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
CharArrayWriter
public class charArrayWriterTest {
public static void main(String[] args) {
CharArrayWriter charArrayWriter = new CharArrayWriter();
charArrayWriter.write(97);
charArrayWriter.write(98);
charArrayWriter.write(99);
System.out.println(charArrayWriter);
charArrayWriter.close();
}
}
BufferedWriter
public class BufferedWriterTest {
public static void main(String[] args) {
BufferedWriter bufferedWriter = null;
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(new File("abc.txt"));
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.append("ccc");
bufferedWriter.newLine();
bufferedWriter.append("bbb");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
处理流
InputStreamReader
public class InputStreamReaderDemo {
public static void main(String[] args) {
File file = new File("abc.txt");
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
try {
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream);
char[] chars = new char[1024];
int length = inputStreamReader.read(chars);
System.out.println(new String(chars,0,length));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OutputStreamWriter
public class OutputStreamWriterDemo {
public static void main(String[] args) {
File file = new File("abc.txt");
OutputStreamWriter outputStreamWriter = null;
FileOutputStream fileOutputStream = null;
try {
long time = System.currentTimeMillis();
fileOutputStream = new FileOutputStream(file);
outputStreamWriter = new OutputStreamWriter(fileOutputStream,"iso8859-1");
outputStreamWriter.write(99);
outputStreamWriter.write("ccc");
outputStreamWriter.write("abcdefg",0,5);
long end = System.currentTimeMillis();
System.out.println(end-time);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}