File打印文件夹-实战
在java.io中有一个File类,其中有很多API,下面将根据这些API进行实战
exists()/isFile()/isDirectory()/isHidden()/lastModified()
canRead()/canWrite()
createNewFile()/createTempFile 创建的是文件
delete()/deleteOnExit()
getAbsolutePath()/getName()/getPath()
getParent()/getParentFile()
list()/listFiles()
mkdir()/mkdirs() 创建的是文件夹
import java.io.File;
public class ShowDirectoryInfo{
File file = new File("C:/Program Files");
initTab = getTabCount(file);
showFileInfo(file);
}
private static int initTab=0;
public static void showFileInfo(File file){
String fileName = file.getName();
printTab(getTabCount(File));
System.out.println(fileName);
if(file.isDirectory()){
File[] childFiles=file.listFiles();
for(int i = 0;i<child.length;i++){
File childFile=childFile[i];
showFileInfo(childFile);
}
}
}
private static void printTab(int tabCount){
for(int i = 0;i<tabCount-initTab;i++){
System.out.print("\t");
}
}
private static int getTabCount(File file){
String absPath=file.getAbsolutePath();
return absPath.spilt("\\\\").length;
}
字节流
当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作。当把持久设备上的数据读取
到内存中的这个动作称为输入(读)Input操作。因此我们把这种输入和输出动作称为IO操作。
流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
输入流代码演示
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
public class InputStreamTest{
public static void main(String[] args) {
InputStream is=null;
try {
is = new FileInputStream("D:/hello.txt");
byte[] bytes=new byte[10];
int len=0;
while((len=is.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
实战,通过io流来复制图片
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 IOTest1 {
public static void main(String[] args) {
InputStream is=null;;
OutputStream os=null;
try {
is=new FileInputStream("D:/dog.jpg");
os=new FileOutputStream("D:/dog.jpg2");
int len=-1;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1) {
os.write(bytes, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(is!=null) {
is.close();
}
if(os!=null) {
os.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符流
FileWriter: 写入文件
FileReader: 读取文件
文件中存在字符的时候使用字符流传送数据可能会出现乱码
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
public class IOTest2{
public static void main(String[] args) throws Exception {
Reader reader = new FileReader("D:/hello.txt");
char[] chars = new char[2];
int len = -1;
while((len = reader.read(chars))!=-1);{
System.out.println(new String(chars,0,len));
}
reader.close();
Writer writer = new FileWriter("D:/hi.txt");
writer.write("我要写入这几个字符:dwqdwddqd");
writer.close();
}
}
转换流和缓冲流
public class IOTest3 {
public static void main(String[] args) throws Exception {
InputStream is=new FileInputStream("D:/hi.txt");
Reader reader=new InputStreamReader(is);
BufferedReader br=new BufferedReader(reader);
BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream("D:/hi.txt")));
String str = null;
while((str=br.readLine())!=null){
System.out.println(str);
}
while((str=br1.readLine())!=null){
System.out.println(str);
}
}
}
打印流
包括PrintStream和PrintWriter,想要熟练掌握IO流就需要多看帮助文档,看各种API的用法。
public class IOTest4 {
public static void main(String[] args) {
PrintStream ps=null;
try {
ps=new PrintStream("D:/ps.txt");
ps.print("hello");
ps.print(" world");
ps.println("!");
ps.printf("%s%d","大家好我今年",20);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(ps!=null) {
ps.close();
}
}
}