- 核心类
- File–文件类
public class PathDemo01 {
public static void main(String[] args) {
String path = "C:/Users/GK/Desktop/IO.jpg";
path = "IO.jpg";
System.out.println(File.separatorChar);
File src = new File(path);
System.out.println(src.getAbsolutePath());
System.out.println(System.getProperty("user.dir"));
src = new File("C:/Users/GK/Desktop","IO.jpg");
src = new File(new File("C:/Users/GK/Desktop"),"IO.jpg");
System.out.println(src.length());
}
}
package org.gk;
import java.io.File;
import java.io.IOException;
public class FileDemo03 {
public static void main(String[] args) throws IOException {
File src = new File("C:/Users/GK/Desktop","IO.jpg");
System.out.println("name: " + src.getName());
System.out.println("path: " + src.getPath());
System.out.println("absolutepath: " + src.getAbsolutePath());
System.out.println("parent: " + src.getParent());
System.out.println(src.getParentFile().getAbsolutePath());
System.out.println("ifexist: " + src.exists());
System.out.println("是否文件:" + src.isFile());
System.out.println("是否为文件夹:" + src.isDirectory());
if(null == src || !src.exists()) {
System.out.println("notExist: " + src.length());
}else {
if(src.isFile()) {
System.out.println("fileLength: " + src.length());
}else {
System.out.println("00000000");
}
}
File src01 = new File("C:/Users/GK/Desktop/aa.txt");
boolean flag = src01.createNewFile();
System.out.println(flag);
boolean flag01 = src.delete();
System.out.println(flag01);
}
}
package org.gk;
import java.io.File;
public class DirTest {
public static void main(String[] args) {
File dir = new File("C:/Users/GK/Desktop/dir/test");
boolean flag = dir.mkdirs();
System.out.println(flag);
File dir01 = new File("C:/Users/GK/Desktop");
String[] subName = dir01.list();
for(String s : subName) {
System.out.println(s);
}
File[] subobj = dir01.listFiles();
for(File f : subobj) {
System.out.println(f.getAbsolutePath());
}
File[] lr = dir01.listRoots();
for(File f : lr) {
System.out.println(f.getAbsolutePath());
}
printNum(1,99);
}
public static void printNum(int n,int m) {
if(n>m) {
return;
}
System.out.println(n);
printNum(n+1,m);
}
}
package org.gk;
import java.io.File;
public class DirCount {
private long len;
private File src;
private int fileSize;
private int dirSize;
public DirCount(String path) {
this.src = new File(path);
count(this.src);
}
private void count(File src) {
if(null!=src && src.exists()) {
if(src.isFile()) {
len+=src.length();
this.fileSize++;
}else {
for(File f : src.listFiles()) {
count(f);
}
this.dirSize++;
}
}
}
public static void main(String[] args) {
DirCount dir = new DirCount("C:/Users/GK/Desktop");
System.out.println(dir.getLen());
System.out.println(dir.getDirSize());
}
public long getLen() {
return len;
}
public int getFileSize() {
return fileSize;
}
public int getDirSize() {
return dirSize;
}
}
package org.gk;
import java.io.UnsupportedEncodingException;
public class ContentEncode {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "hello五";
String str01 = "hello五";
byte[] b = str.getBytes();
System.out.println(b.length);
b = str01.getBytes("UTF-16LE");
System.out.println(b.length);
String msg = new String(b,0,b.length,"utf-16le");
System.out.println(msg);
}
}
- InputStream–字节输入流
package com.gk;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest01 {
public static void main(String[] args) {
File src = new File("C:/Users/GK/Desktop/aa.txt");
InputStream is = null;
try {
is = new FileInputStream(src);
int temp;
while((temp=is.read()) != -1) {
System.out.print((char)temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(is!=null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.gk;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest02 {
public static void main(String[] args) {
File src = new File("C:/Users/GK/Desktop/aa.txt");
InputStream is = null;
try {
is = new FileInputStream(src);
byte[] flush = new byte[1024*10];
int len = -1;
while((len=is.read(flush)) != -1) {
String str = new String(flush,0,len);
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(is!=null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- OutputStream–
package com.gk;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class IOTest03 {
public static void main(String[] args) {
File dest = new File("C:/Users/GK/Desktop/f.txt");
OutputStream os = null;
try {
os = new FileOutputStream(dest,true);
String str = "hello";
byte[] datas = str.getBytes();
os.write(datas,0,datas.length);
os.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
try {
if (os != null) {
os.close();
}
} catch (Exception e) {
}
}
}
}
package com.gk;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOTest4 {
public static void main(String[] args) {
cpFile("C:/Users/GK/Desktop/test.jpg","C:/Users/GK/Desktop/test1.jpg");
}
public static void cpFile(String srcPath, String destPath) {
File src = new File(srcPath);
File dest = new File(destPath);
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest);
byte[] datas = new byte[10];
int len = -1;
while((len=is.read(datas)) != -1) {
os.write(datas,0,len);
}
os.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
try {
if (os != null) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
- Reader–字符输入流
package com.gk;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class IOTest5 {
public static void main(String[] args) {
File src = new File("C:/Users/GK/Desktop/aa.txt");
Reader rd = null;
try {
rd = new FileReader(src);
char[] flush = new char[2];
int len = -1;
while((len=rd.read(flush)) != -1) {
String str = new String(flush,0,len);
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(rd!=null) {
rd.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- Writer–
package com.gk;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class IOTest6 {
public static void main(String[] args) {
File dest = new File("C:/Users/GK/Desktop/f.txt");
Writer wt = null;
try {
wt = new FileWriter(dest);
wt.append("aaaaaa").append("2222");
wt.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
try {
if (wt != null) {
wt.close();
}
} catch (Exception e) {
}
}
}
}
- Closeable–关闭流接口
o - Flushable–刷新流接口
o - Serializable–序列化接口