
本文共 21293 字,大约阅读时间需要 70 分钟。
������������
IO���������/������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
IO������������������������������������������IO���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������
File ���������������������������������������File������������������������������������������������File���������������������������������File ���������������������������������������������������������������������������������������������������������������������
2.1 ������������
File(String pathname)���������������������������File������
File(File parent, String child):���������������File������������������������/������������File���
2.2 ������������
//������������������������������File������File file = new File("1.txt");//������������System.out.println(file.createNewFile());File file2 = new File("temp"); //���������������������������System.out.println(file2.mkdir());
//������������������������������������������file2.renameTo(new File("temp2"));//���������������������������file2.delete();
//���������������������������������System.out.println(file.exists());//���������������������System.out.println(file.isFile());//���������������������System.out.println(file.isDirectory());//���������������������System.out.println(file.isAbsolute());//���������������������������System.out.println(file.canRead());//���������������������������System.out.println(file.canWrite());
//������������������������System.out.println(file.length());//������������������������System.out.println(file.getName());//���������������������������������System.out.println(file.getPath());//���������������������������������System.out.println(file.getAbsolutePath());//���������������������System.out.println(file.getAbsoluteFile().getParent());//������������������������������������������������String[] list = file1.list();for (String fileName : list) { System.out.println(fileName);}//������������������������������������������������������File������File[] files = file1.listFiles();//������������������������������File[] listRoots = File.listRoots();for (File root : listRoots) { System.out.println(root);}
������
������������������������������������������������������������
������������������������������������������������������������������������������
������������������������������������������������������������
������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������8������������������������������16���������������
3.2 InputStream���Reader
InputStream���Reader���������������������������������������������������������������InputStream������������������
int read() ���������������������������������������������������������������������
int read(byte b[])������������������������������b.length���������������������������������������������b������
int read(byte b[], int off, int len)������������������������������len���������������������������������������������b���������������������������off������������
Reader���������������������
int read() ���������������������������������������������������������������������
int read(char cbuf[])������������������������������cbuf.length���������������������������������������������cbuf������
int read(char cbuf[], int off, int len)������������������������������len���������������������������������������������cbuf���������������������������off������������
���������������������������������������������������������������������������
InputStream inputStream = new FileInputStream("StreamTest.java");
byte[] bytes = new byte[1024];int hasRead = 0;while ((hasRead = inputStream.read(bytes)) > 0) { System.out.println(new String(bytes, 0, hasRead));}inputStream.close();
OutputStream���Writer���������������������������������������������������������OutputStream������������������
void write(int b)������������������������������
void write(byte b[])���������������������������������������
void write(byte b[], int off, int len)���������������������������off���������len���������������������
Writer������������������������
void write(int b)������������������������������
void write(char buf[])���������������������������������������
void write(char cubf[], int off, int len)���������������������������off���������len���������������������
������Writer���������������������������������������������������String ������������������������������������
void write(String str)������str������������������������
void write(String str, int off, int len)������str���off���������������������len���������������
FileWriter fileWriter = new FileWriter("test.txt");fileWriter.write("���������������������\r\n");fileWriter.write("���������������������\r\n");fileWriter.write("���������������������\r\n");fileWriter.write("���������������������\r\n");fileWriter.close();
������������������������������������������������������������IO���������������������������������������������������������
������
������ | ��������������� | ��������������� | ��������������� | ��������������� |
---|---|---|---|---|
������������ | InputStream | OutputStream | Reader | Writer |
������������ | FileInputStream | FileOutputStream | FileReader | FileWriter |
������������ | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
������������ | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
��������������� | StringReader | StringWriter | ||
��������� | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
��������� | InputStreamReader | OutputStreamWriter | ||
��������� | ObjectInputStream | ObjectOutputStream | ||
��������� | FilterInputStream | FilterOutputStream | FilterReader | FilterWriter |
��������� | PrintStream | PrintWriter | ||
��������������� | PushbackInputStream | PushbackReader | ||
��������� | DataInputStream | DataOutputStream |
������������������/���������������������������������������������������������������������������/������������������������������������������������������������������
���������������������������������������������������������������������������InputStreamReader���������������������������������������������OutputStreamWriter���������������������������������������������System.in������������������������������������������������������������������������������������������������������������������������������InputStreamReader������������������������������������Reader������������������������������������������BufferedReader���������������������������������
//������System.in���������Reader ������InputStreamReader inputStreamReader = new InputStreamReader(System.in);//������Reader���������BufferedReaderBufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line = null;while ((line = bufferedReader.readLine()) != null) { if (line.equals("exit")) { System.exit(1); } System.out.println("���������������������" + line);}
BufferedReader������������������������������������������������������������������������������������
���������������PushbackInputStream���PushbackReader������������������������������
void unread(int b) ������������������/���������������������������������������������������������������������������������
void unread(byte[] b/char[] b, int off, int len) ������������������/������������������off������������������len������/������������������������������������������������������������������������������������������
void unread(byte[] b/char[])������������������/���������������������������������������������������������������������������������������������
������������������������������������������������������������unread()������������������������������������������������������������������������������������read������������������������������������������������������������������������������������������������������������read()���������������������������������������������������
//������PushbackReader������������������������������������������64PushbackReader pushbackReader = new PushbackReader(new FileReader("StreamTest.java"), 64);char[] buf = new char[32];//������������������������������������������String lastContent = "";int hasRead = 0;//������������������������while ((hasRead = pushbackReader.read(buf)) > 0) { //������������������������������������ String content = new String(buf, 0, hasRead); int targetIndex = 0; if ((targetIndex = (lastContent + content).indexOf("new PushbackReader")) > 0) { //������������������������������������������������������ pushbackReader.unread((lastContent + content).toCharArray()); //���������������������������targetIndex���char������ if (targetIndex > 32) { buf = new char[targetIndex]; } //��������������������������������� pushbackReader.read(buf, 0, targetIndex); //��������������������� System.out.print(new String(buf, 0, targetIndex)); System.exit(0); } else { //��������������������������� System.out.print(lastContent); //������������������������������������������ lastContent = content; }}
RandomAccessFile���Java������/������������������������������������������������������������������������������������������������������������������������������������������������������������������RandomAccessFile������������������������������������������
RandomAccessFile���������������������������������������������������������������������������������������������������������0���������������/������N���������������������������������������N������������������RandomAccessFile���������������������������������������������������������������������������������������������
getFilePointer()���������������������������������������������
seek(long pos)���������������������������������pos���������
RandomAccessFile������������������������
RandomAccessFile(File file, String mode)���������File��������������������������� RandomAccessFile(String name, String mode)������������������������������������
������������������������mode������������������������������������4���������
r:���������������������������
rw:������������������������������������������������������������������
rws:������������������������������������������������������������������������������������������������������������������������������
rwd:���������������������������������������������������������������������������������������������������������������
RandomAccessFile raf = new RandomAccessFile("StreamTest.java", "r");System.out.println("������������������������������" + raf.getFilePointer());//������������������raf.seek(300);byte[] buf = new byte[1024];int hasRead = 0;while ((hasRead = raf.read(buf)) > 0) { //������������ System.out.println(new String(buf, 0, hasRead));}//������������RandomAccessFile randomAccessFile=new RandomAccessFile("out.txt","rw");randomAccessFile.setLength(randomAccessFile.length());randomAccessFile.write("������������������\r\n".getBytes());
���������������������
���������������������������������������������java������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������java���������
������������������������������������������������������Serializable������Externalizable������������������������������������������������������������������������������������������������������������������������JavaWeb������������������������������������
6.1 ������������������������������
���������������������������������������������������
������������������������
@Datapublic class Person implements Serializable { private int age; private String name; public Person(String name, int age) { System.out.println("���������������������"); this.age = age; this.name = name; }}
������������������������������������
//���������������ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.txt"));Person person = new Person("������", 10);//���person���������������objectOutputStream.writeObject(person);//���������������ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("object.txt"));try { //������������ Person p = (Person) objectInputStream.readObject(); System.out.println(p);} catch (ClassNotFoundException e) { e.printStackTrace();}
������������������������������Java������������������������java���������������������������������������������������������class������������������������������������������������������������������������������������������������������������������������������Java���������
���������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������transient���������������������������
������NIO
���������������IO������������������������������������������������������������������������������������������������������������������������������NIO������������������������������������java.nio���������
���IO ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������IO������������
���IO������������������������Channel���������������Buffer���������������Channel������������InputStream���OutputStream������������������������������������map���������������������������������������������������������������������������IO������������������������������Buffer������������������������������������������������������������Channel���������������������������������������Buffer���������������������������Buffer������������
Buffer������������������������������������������ByteChannel���CharBuffer���Buffer������������������������������������������XXXBuffer allocate(int capacity) ���������������������
CharBuffer allocate = CharBuffer.allocate(8);
Buffer������������������������
���������capacity������������������������������������buffer���������������������������������������������������������������������������������
���������limit������������limit������������������������������������������������
���������position������������������������������������������������������������������������������������IO���������������
Buffer���������������������������������������������������������buffer������position���0���������limit���capacity,���������������������position���������������
���Buffer������������������������flip������������������������������limit���������position,position���������0���������������������������������������������������������������������������������������������������������������clear������������������������position���������0,limit���������capacity,������������������������������������������
������������������������������Buffer������������������������������put()���get()���������������������������������������������������������������������������������������������������������
������������Buffer���position���������������������������������������������������������position������������
���������������������������������������������������������������������position������������
//������bufferCharBuffer buffer = CharBuffer.allocate(10);System.out.println("capacity: " + buffer.capacity());System.out.println("limit:" + buffer.limit());System.out.println("position:" + buffer.position());//������������buffer.put('a');buffer.put('b');buffer.put('c');System.out.println("������������������position:" + buffer.position());buffer.flip();System.out.println("������flip������limit���" + buffer.limit());System.out.println("position:" + buffer.position());System.out.println("���������������������" + buffer.get());System.out.println("������������������position���" + buffer.position());buffer.clear();System.out.println("������clear������limit���" + buffer.limit());System.out.println(",position:" + buffer.position());System.out.println("������clear���������������������������" + buffer.get(2));System.out.println("������������������position���������������" + buffer.position());
7.2 Channel
Channel������������������������������������������Channel���������������������������������������������������������Buffer������������������������Channel���������������������������������������Channel���������������������������������FileChannel������������������������
File file = new File("StreamTest.java");//���������������FileChannelFileChannel inChannel = new FileInputStream(file).getChannel();//������������������������FileChannel���������������FileChannel outChannel = new FileOutputStream("a.txt").getChannel();//���FileChannel���������ByteBuffer���MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());Charset charset = Charset.forName("GBK");//������������outChannel.write(buffer);buffer.clear();CharsetDecoder charsetDecoder = charset.newDecoder();//���������CharBuffer������������CharBuffer charBuffer = charsetDecoder.decode(buffer);System.out.println(charBuffer);
7.3 ������������Charset
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Java������������Unicode������������������������������������������������������������������������������������������������������������Unicode������������������������������������������������������������������������������������������
Charset������������������������������������������������������������������Charset������������������������������������������������Charset���forName������������������������������������������CharsetEncoder���CharsetDecoder������������������������������������������������������������������������
SortedMapstringCharsetSortedMap = Charset.availableCharsets();for(String name:stringCharsetSortedMap.keySet()){ System.out.println(name);}//���������������������������CharsetCharset cn = Charset.forName("GBK");//������������������������������������CharsetEncoder cnEncoder = cn.newEncoder();CharsetDecoder cnDecoder = cn.newDecoder();CharBuffer buff = CharBuffer.allocate(8);buff.put('���');buff.put('���');buff.flip();//���buff���������������������������ByteBuffer bbuff = cnEncoder.encode(buff);for (int i = 0; i
Path path = Paths.get(".");System.out.println("path������������������������" + path.getNameCount());System.out.println("path���������������" + path.getRoot());Path path1 = path.toAbsolutePath();System.out.println("path������������������" + path1);//������String������������Path path2 = Paths.get("G:", "test", "codes");System.out.println("path2������������" + path2);System.out.println("StreamTest.java���������������������:" + Files.isHidden(Paths.get("StreamTest.java")));//������������������������ListallLines = Files.readAllLines(Paths.get("StreamTest.java"), Charset.forName("gbk"));System.out.println(allLines);//������������System.out.println("StreamTest.java���������������" + Files.size(Paths.get("StreamTest.java")));List poem = new ArrayList<>();poem.add("���������������������");poem.add("���������������������������");//���������������������Files.write(Paths.get("poem.txt"), poem, Charset.forName("gbk"));
������������Paths���Files���������������������������������������������������������������������������������������������������������������������������������������������������API���
7.5 ������������
java.nio.file.attribute������������������������������������������������������������������������������������������������
BasicFileAttributeView baseView = Files.getFileAttributeView(Paths.get("poem.txt"), BasicFileAttributeView.class);BasicFileAttributes basicFileAttributes = baseView.readAttributes();System.out.println("���������������" + basicFileAttributes.creationTime().toMillis());System.out.println("���������������������" + basicFileAttributes.lastModifiedTime().toMillis());
发表评论
最新留言
关于作者
