IO basic knowledge of java

java IO technology

Core classes (5) and an interface (3)

  1. file: File Class
  2. InputStream: input stream of bytes
  3. OutputStream: byte output stream
  4. Reader: character-input stream
  5. Writer: character-output stream
  6. Closeable: Close stream interface
  7. Flushable: Refresh the stream interface
  8. Serializable: serial interfaces

Input and output of java, java program is said to center.

Input streams: a data source to the program, (InputStream, Reader)
output streams: a program to a destination, (OutputStream, Writer)
node stream: data can be directly read from a data source or destination.
Processing flow (flow packing): not directly connected to a data source or destination, other package stream in order to streamline the operation and performance.

Relationship between the nodes and the process flow stream:
1. The nodes in a first flow line io operation, all the operations must be performed by them,
2. process stream may be processed (operation efficiency or flexibility) of the other stream

But in a different file system separator is different, providing a unified separator in the File class, obtained by File.separator
in java is generally recommended to use "/" to separate

Some common methods of the File class

public class IoStream {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String path="D:\\Java\\myball\\src\\images\\ground.jpg";//java 中会自动加上转义字符
		//但是在不同的系统中文件的分隔符是不同的,在File类中提供了统一的分隔符
		//通过File.separator
		
		//1.在java中一般建议使用“/"来分隔
		String path1="D:/Java/myball/src/images/ground.jpg";
		
		//2.或者是使用File.separator来拼接。
		String path2="D:"+File.separator+"Java"+File.separator+"myball"+File.separator+"src"+File.separator+"images"+File.separator+"ground.jpg";
		System.out.println(path);
		System.out.println(path1);
		System.out.println(path2);
		//1.构建File对象
		File src=new File(path1);
		System.out.println("长度:"+src.length());//图的大小
		//2.
		File src1=new File("D:/Java/myball/src/images","ground.jpg");
		//3.
		File src2=new File(new File("D:/Java/myball/src/images"),"ground.jpg");
		
		System.out.println(src1.getAbsolutePath());
		System.out.println(System.getProperty("user.dir"));
		
		
		System.out.println("名称:"+src1.getName());
		System.out.println("路径名:"+src1.getPath());//获得创建是的路径名
		//如果创建时的是绝对路径,则返回路径,如果是相对路径,则返回相对路径
		System.out.println("绝对路径:"+src1.getAbsolutePath());
		System.out.println("父名称:"+src1.getParent());//返回该文件的前面的所有文件名
		System.out.println("是否存在"+src1.exists());
		System.out.println("是否是文件夹"+src1.isDirectory());
		System.out.println("是否是文件"+src1.isFile());
		src=new File("xxx");
		if(!src.exists()) {
			System.out.println("文件不存在");
		}else {
			System.out.println("文件操作");
		}
		
		File src3=new File("D:/Java/myball/src/images");//文件夹
		System.out.println("文件夹的长度:"+src3.length());//0
		
		//文件夹的长度需要递归计算子孙文件
		
		//createNewFile()表示的是当文件不存在的时候才创建。如果文件创建失败的话,就创建失败。
		File src4=new File("D:/Java/myball/src/images/java.txt");
		System.out.println(src4.createNewFile());//createNewFile()不存在才创建
		//createNewFile()只能创建文件,不创建文件夹。
		System.out.println(src4.delete());//删除已经存在的文件。
		
		
		
		
		
	}

}

java directories

Create a directory object:
mkdir: ensure that there is the parent directory
mkdirs: You can create parent directory

Listed under a
1.list (): lists the name of the next level directory

2.listFile (): objects listed in a file

3.listRoots (): lists all the letters;

Traversing File test code

public class TestDrectory {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File dir=new File("D:/Java/myball/src");
		String[] fileNames=dir.list();
		for(String s:fileNames) {
			System.out.println(s);
		}
		
		
		
		File[] files=dir.listFiles();
		for(File s:files) {
			System.out.println(s);
		}
		
		
		File[] roots=dir.listRoots();
		for(File s:roots) {
			System.out.println(s);
		}
	}

}

Recursive folder traversal,

public class TestDrectory {
	//利用递归遍历文件夹,
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File src=new File("D:/Java/myball/src");
		printName(src,0);
		
	}
	public static void printName(File src,int level) {
		
		for(int i=0;i<level;i++) {
			System.out.print("  ");
		}
		System.out.println(src.getName());
		
		if(src==null||!src.exists()) {//递归头
			return;
		}else if(src.isDirectory()) {//如果是目录
			for(File s:src.listFiles()) {
				printName(s,level+1);//递归体。
			}
			
		}
	}

}

// Get the size of the folder.

public class TestDrectory {
	//获取文件夹的大小。
	
	private static long len=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		File src=new File("D:/Java/myball/src");
		count(src);
		System.out.println(len);
		
	}
public static void count(File src) {
		if(src!=null||src.exists()) {//如果存在
			if(src.isFile()) {//计算大小,递归头。
				len+=src.length();
			}else {//子孙及
				for(File s:src.listFiles()) {
					count(s);//递归体
				}
			}
		}
	}

}

The number of object-oriented implementation file size calculation method, calculating the number of folders, file

public class TestDrectory {
	//获取文件夹的大小。
	
	private  long len=0;
	private int dirCount;
	private int fileCount;
	private String path;
	private File src;
	public TestDrectory(String path) {
		super();
		this.path = path;
		this.src =new File(path);
		count(src);
	}
	
	public void count(File src) {
		if(src!=null||src.exists()) {//如果存在
			if(src.isFile()) {//计算大小,递归头。
				this.fileCount++;
				len+=src.length();
			}else {//子孙及
				this.dirCount++;
				for(File s:src.listFiles()) {
					count(s);//递归体
				}
			}
		}
	}

	public long getLen() {
		return len;
	}
	public int getDirCount() {
		return dirCount;
	}
	public int getFileCount() {
		return fileCount;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		String path="D:/Java/myball/src";
		TestDrectory td=new TestDrectory(path);
		System.out.println(td.len);
		System.out.println(td.dirCount);
		System.out.println(td.fileCount);
		
		
	}

}

java character encoding

Different character sets, will, during encoding and decoding, according to different rules during processing in accordance with different rules.

If decoding is performed, the length of the character set does not match the character set or the coding does not match, the phenomenon will be garbled

public class TestDrectory {
	
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		
		//编码,
		String path="姓名";//GBK编码方式,是固定每个字符两个字节
		byte[] datas=path.getBytes();
		System.out.println(datas.length);
		//解码
		String path1=new String(datas);
		System.out.println(path1);
		//会出现乱码。当datas的长度不匹配或者是编码的字符集不匹配时,会出现乱码现象
		String path2=new String(datas,1,datas.length-1,"utf8");
		System.out.println(path2);
		
		
	}

}

Published 43 original articles · won praise 11 · views 2582

Guess you like

Origin blog.csdn.net/weixin_43328816/article/details/104316635