a

 

Closeable是接口				**
DataInput是接口				*
DataOutput是接口			*
Externalizable是接口		*
FileFilter是接口			**
FilenameFilter是接口		**
Flushable是接口				***
ObjectInput是接口			**
ObjectInputValidation是接口	*
ObjectOutput是接口			**
ObjectStreamConstants是接口	*
Serializable是接口			*****

FilePermission的父类不在io包中:java.security.Permission						***
IOError的父类不在io包中:java.lang.Error										*
IOException的父类不在io包中:java.lang.Exception								*
SerializablePermission的父类不在io包中:java.security.BasicPermission		*
UncheckedIOException的父类不在io包中:java.lang.RuntimeException				*

java.lang.Object
	java.io.Bits															*,default
	java.io.Console		接口:java.io.Flushable								**
	java.io.DefaultFileSystem												*,default
	java.io.DeleteOnExitHook												*,default
	java.io.ExpiringCache													*,default
	java.io.File		接口:java.io.Serializable、java.lang.Comparable		*****
	java.io.FileDescriptor
	java.io.FileSystem
		java.io.WinNTFileSystem
	java.io.InputStream		接口:java.io.Closeable
		java.io.ByteArrayInputStream
		java.io.FileInputStream
		java.io.FilterInputStream
			java.io.BufferedInputStream
			java.io.DataInputStream		接口:java.io.DataInput
			java.io.LineNumberInputStream
			java.io.PushbackInputStream
		java.io.ObjectInputStream		接口:java.io.ObjectInput、java.io.ObjectStreamConstants
		java.io.PipedInputStream
		java.io.SequenceInputStream
		java.io.StringBufferInputStream
	java.io.ObjectStreamClass		接口:java.io.Serializable
	java.io.ObjectStreamField		接口:java.lang.Comparable
	java.io.OutputStream		接口:java.io.Closeable、java.io.Flushable
		java.io.ByteArrayOutputStream
		java.io.FileOutputStream
		java.io.FilterOutputStream
			java.io.BufferedOutputStream
			java.io.DataOutputStream		接口:java.io.DataOutput
			java.io.PrintStream		接口:java.lang.Appendable、java.io.Closeable
		java.io.ObjectOutputStream		接口:java.io.ObjectOutput、java.io.ObjectStreamConstants
		java.io.PipedOutputStream
	java.io.RandomAccessFile		接口:java.io.DataOutput、java.io.DataInput、java.io.Closeable
	java.io.Reader		接口:java.lang.Readable、java.io.Closeable
		java.io.BufferedReader
			java.io.LineNumberReader
		java.io.CharArrayReader
		java.io.FilterReader
			java.io.PushbackReader
		java.io.InputStreamReader
			java.io.FileReader
		java.io.PipedReader
		java.io.StringReader
	java.io.SerialCallbackContext
	java.io.StreamTokenizer
	java.io.Writer		接口:java.lang.Appendable、java.io.Closeable、java.io.Flushable
		java.io.BufferedWriter
		java.io.CharArrayWriter
		java.io.FilterWriter
		java.io.OutputStreamWriter
			java.io.FileWriter
		java.io.PipedWriter
		java.io.PrintWriter
		java.io.StringWriter
package com.simom.other.classc;

import java.io.IOError;

/**
 * 1颗星<br/>
 * 当发生严重的 I/O 错误时,抛出此错误。
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class IOErrorClass {

	public void init() {
		IOError io = null;
	}
}
package com.simom.other.classc;

import java.io.IOException;

/**
 * 1颗星
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class IOExceptionClass {

	public void init() {
		IOException io = null;
	}
}
package com.simom.other.classc;

import java.io.SerializablePermission;

/**
 * 1颗星
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class SerializablePermissionClass {

	public void init() {
		SerializablePermission s = null;
	}
}
package com.simom.other.classc;

import java.io.UncheckedIOException;

/**
 * 1颗星
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class UncheckedIOExceptionClass {

	public void init(){
		UncheckedIOException un = null;
	}
}
package com.simom.io.classc;

/**
 * 1颗星<br/>
 * 该类为default,唯一的方法getFileSystem:获取windows的默认文件系统对象
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class DefaultFileSystemClass {

}
package com.simom.io.classc;

/**
 * 1颗星
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class DeleteOnExitHookClass {

	public void init() {
	}
}
package com.simom.io.classc;

/**
 * 1颗星
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class ExpiringCacheClass {

	public void init() {
	}
}
package com.simom.io.classc;

import java.io.File;

import org.junit.Test;

import com.simom.utils.ClassUtil;
import com.simom.utils.WinNTFileSystemUtil;

/**
 * 5颗星<br/>
 * 
 * @author DENGSHAOXIANG787
 * @date 2018年5月15日
 */
public class FileClass {
	
	private WinNTFileSystemUtil util = new WinNTFileSystemUtil();

	@Test
	public void init() {
		File file = new File("src/main/resources/a.txt");
		System.out.println(file.getName());
	}
	
	@Test
	public void init2() {
		File file = new File("src/main/resources","a.txt");
		System.out.println(file.getName());//a.txt
		System.out.println(file.getAbsolutePath());//D:\文件\邓邵湘\Workspace-sts\java-io-source\src\main\resources\a.txt
		file = new File("","a.txt");
		System.out.println(file.getName());//a.txt
		System.out.println(file.getAbsolutePath());//D:\a.txt
	}
	
	@Test
	public void getName(){
		File file = new File("c:/a");
		System.out.println(file.getName());//a
	}
	
	@Test
	public void getParent(){
		File file = new File("src\\main\\resources\\a.txt");
		System.out.println(file.getParent());//src/main/resources
	}
	
	@Test
	public void getAbsolutePath(){
		File file = new File("c:a.txt");
		System.out.println(file.getAbsolutePath());
		file = new File("c:\\a.txt");
		System.out.println(file.getAbsolutePath());
	}
	
	@Test
	public void structure(){
		ClassUtil.constructors(File.class);
		ClassUtil.declaredFields(File.class);
//		ClassUtil.declaredMethods(File.class);
	}

	@Test
	public void normalize() {
//		String pathName = "src/main/resources/a.txt/";
		String pathName = "\\\\src:";
		String path = util.normalize(pathName);
		int prefixLength = util.prefixLength(path);
		System.out.println("path:" + path + ",prefixLength:" + prefixLength);
		File file = new File(pathName);
		System.out.println(file.getAbsolutePath());
	}
	
	@Test
	public void resolve(){
		String path = util.resolve("", "a.txt");
		System.out.println(path);
		path = util.resolve("src/main/resources","a.txt");
		System.out.println(path);
	}
	
	@Test
	public void getUserPath(){
		System.out.println(System.getProperty("user.dir"));
		System.out.println(util.getUserPath());
	}

}
package com.simom.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class ClassUtil {

	public static void constructors(Class<?> c) {
		System.out.println("=============Constructors=============");
		Constructor[] constructors = c.getConstructors();
		StringBuffer sb = null;
		for (Constructor constructor : constructors) {
			sb = new StringBuffer();
			sb.append(constructor.getName()).append("(");
			Parameter[] parameters = constructor.getParameters();
			for (Parameter parameter : parameters) {
				sb.append(parameter.getType().getSimpleName()).append(" ");
				sb.append(parameter.getName()).append(",");
			}
			sb.append(")");
			System.out.println(sb);
		}
		System.out.println("=============Constructors=============");
	}

	public static void declaredMethods(Class<?> c) {
		System.out.println("=============DeclaredMethods=============");
		Method[] methods = c.getDeclaredMethods();
		StringBuffer sb = null;
		for (Method method : methods) {
			sb = new StringBuffer();
			sb.append(method.getReturnType());
			sb.append(" ");
			sb.append(method.getName());
			sb.append("(");
			Parameter[] parameters = method.getParameters();
			for (Parameter parameter : parameters) {
				sb.append(parameter.getType().getSimpleName()).append(" ");
				sb.append(parameter.getName()).append(",");
			}
			sb.append(")");
			System.out.println(sb);
		}
		System.out.println("=============DeclaredMethods=============");
	}

	public static void declaredFields(Class<?> c) {
		System.out.println("=============DeclaredFields=============");
		Field[] fields = c.getDeclaredFields();
		for (Field field : fields) {
			if("boolean".equals(field.getType().getSimpleName())){
				System.out.print("");
			}
			System.out.println(field.getType().getSimpleName() + " " + field.getName());
		}
		System.out.println("=============DeclaredFields=============");
	}

}
package com.simom.utils;

public class WinNTFileSystemUtil {

	private final char slash = '\\';
	private final char semicolon = ';';
	private final char altSlash = '/';

	private boolean isSlash(char c) {
		return (c == '\\') || (c == '/');
	}

	private boolean isLetter(char c) {
		return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
	}

	public String getDefaultParent() {
		return ("" + slash);
	}

	public String normalize(String path) {
		int n = path.length();
		char slash = this.slash;
		char altSlash = this.altSlash;
		char prev = 0;
		for (int i = 0; i < n; i++) {
			char c = path.charAt(i);
			if (c == altSlash)
				return normalize(path, n, (prev == slash) ? i - 1 : i);
			if ((c == slash) && (prev == slash) && (i > 1))
				return normalize(path, n, i - 1);
			if ((c == ':') && (i > 1))
				return normalize(path, n, 0);
			prev = c;
		}
		if (prev == slash)
			return normalize(path, n, n - 1);
		return path;
	}

	/*
	 * Normalize the given pathname, whose length is len, starting at the given offset; everything before this offset is
	 * already normal.
	 */
	private String normalize(String path, int len, int off) {
		if (len == 0)
			return path;
		if (off < 3)
			off = 0; /* Avoid fencepost cases with UNC pathnames */
		int src;
		char slash = this.slash;
		StringBuffer sb = new StringBuffer(len);

		if (off == 0) {
			/* Complete normalization, including prefix */
			src = normalizePrefix(path, len, sb);
		} else {
			/* Partial normalization */
			src = off;
			sb.append(path.substring(0, off));
		}

		/*
		 * Remove redundant slashes from the remainder of the path, forcing all slashes into the preferred slash
		 */
		while (src < len) {
			char c = path.charAt(src++);
			if (isSlash(c)) {
				while ((src < len) && isSlash(path.charAt(src)))
					src++;
				if (src == len) {
					/* Check for trailing separator */
					int sn = sb.length();
					if ((sn == 2) && (sb.charAt(1) == ':')) {
						/* "z:\\" */
						sb.append(slash);
						break;
					}
					if (sn == 0) {
						/* "\\" */
						sb.append(slash);
						break;
					}
					if ((sn == 1) && (isSlash(sb.charAt(0)))) {
						/*
						 * "\\\\" is not collapsed to "\\" because "\\\\" marks the beginning of a UNC pathname. Even
						 * though it is not, by itself, a valid UNC pathname, we leave it as is in order to be
						 * consistent with the win32 APIs, which treat this case as an invalid UNC pathname rather than
						 * as an alias for the root directory of the current drive.
						 */
						sb.append(slash);
						break;
					}
					/*
					 * Path does not denote a root directory, so do not append trailing slash
					 */
					break;
				} else {
					sb.append(slash);
				}
			} else {
				sb.append(c);
			}
		}

		String rv = sb.toString();
		return rv;
	}

	/*
	 * A normal Win32 pathname contains no duplicate slashes, except possibly for a UNC prefix, and does not end with a
	 * slash. It may be the empty string. Normalized Win32 pathnames have the convenient property that the length of the
	 * prefix almost uniquely identifies the type of the path and whether it is absolute or relative:
	 * 
	 * 0 relative to both drive and directory 1 drive-relative (begins with '\\') 2 absolute UNC (if first char is
	 * '\\'), else directory-relative (has form "z:foo") 3 absolute local pathname (begins with "z:\\")
	 */
	private int normalizePrefix(String path, int len, StringBuffer sb) {
		int src = 0;
		while ((src < len) && isSlash(path.charAt(src)))
			src++;
		char c;
		if ((len - src >= 2) && isLetter(c = path.charAt(src)) && path.charAt(src + 1) == ':') {
			/*
			 * Remove leading slashes if followed by drive specifier. This hack is necessary to support file URLs
			 * containing drive specifiers (e.g., "file://c:/path"). As a side effect, "/c:/path" can be used as an
			 * alternative to "c:/path".
			 */
			sb.append(c);
			sb.append(':');
			src += 2;
		} else {
			src = 0;
			if ((len >= 2) && isSlash(path.charAt(0)) && isSlash(path.charAt(1))) {
				/*
				 * UNC pathname: Retain first slash; leave src pointed at second slash so that further slashes will be
				 * collapsed into the second slash. The result will be a pathname beginning with "\\\\" followed (most
				 * likely) by a host name.
				 */
				src = 1;
				sb.append(slash);
			}
		}
		return src;
	}

	public int prefixLength(String path) {
		char slash = this.slash;
		int n = path.length();
		if (n == 0)
			return 0;
		char c0 = path.charAt(0);
		char c1 = (n > 1) ? path.charAt(1) : 0;
		if (c0 == slash) {
			if (c1 == slash)
				return 2; /* Absolute UNC pathname "\\\\foo" */
			return 1; /* Drive-relative "\\foo" */
		}
		if (isLetter(c0) && (c1 == ':')) {
			if ((n > 2) && (path.charAt(2) == slash))
				return 3; /* Absolute local pathname "z:\\foo" */
			return 2; /* Directory-relative "z:foo" */
		}
		return 0; /* Completely relative */
	}

	public String resolve(String parent, String child) {
		if (parent.equals("")) {
			return resolves(getDefaultParent(), normalize(child));
		} else {
			return resolves(normalize(parent), normalize(child));
		}
	}

	private String resolves(String parent, String child) {
		int pn = parent.length();
		if (pn == 0)
			return child;
		int cn = child.length();
		if (cn == 0)
			return parent;

		String c = child;
		int childStart = 0;
		int parentEnd = pn;

		if ((cn > 1) && (c.charAt(0) == slash)) {
			if (c.charAt(1) == slash) {
				/* Drop prefix when child is a UNC pathname */
				childStart = 2;
			} else {
				/* Drop prefix when child is drive-relative */
				childStart = 1;

			}
			if (cn == childStart) { // Child is double slash
				if (parent.charAt(pn - 1) == slash)
					return parent.substring(0, pn - 1);
				return parent;
			}
		}

		if (parent.charAt(pn - 1) == slash)
			parentEnd--;

		int strlen = parentEnd + cn - childStart;
		char[] theChars = null;
		if (child.charAt(childStart) == slash) {
			theChars = new char[strlen];
			parent.getChars(0, parentEnd, theChars, 0);
			child.getChars(childStart, cn, theChars, parentEnd);
		} else {
			theChars = new char[strlen + 1];
			parent.getChars(0, parentEnd, theChars, 0);
			theChars[parentEnd] = slash;
			child.getChars(childStart, cn, theChars, parentEnd + 1);
		}
		return new String(theChars);
	}
	
	public String getUserPath() {
        /* For both compatibility and security,
           we must look this up every time */
        return normalize(System.getProperty("user.dir"));
    }
}

 

转载于:https://my.oschina.net/u/2247227/blog/1812345

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值