天天看點

String類構造器

public static void main(String[] args) throws IOException {
		
		System.out.println("***************** s1 ****************");
//		public String(String original)
		String s1 = new String("abc");
		System.out.println("構造器 String(String original) : "+s1);
		
		System.out.println("***************** s2 ****************");
//		public String(char value[]) 
		char[] c1 = new char[3];
		c1[0]='a';
		c1[1]='b';
		c1[2]='c';
		String s2 = new String(c1);
		System.out.println("構造器 String(char value[]) 方式1: "+s2);
		
		System.out.println("***************** s3 ****************");
		String temp = "abc";
		char[] c2 = temp.toCharArray();
		String s3 = new String(c2);
		System.out.println("構造器 String(char value[]) 方式2: "+s3);
		
		System.out.println("***************** s4 ****************");
//		String(char value[], int offset, int count) 
		String s4 = new String(c2,0,c2.length);
		System.out.println("構造器 String(char value[], int offset, int count) : "+s4);
		
		System.out.println("***************** s5 ****************");
//		String(int[] codePoints, int offset, int count)
		int[] ary ={65,66,67,68,97,98};
		String s5 = new String(ary, 2, 3);
		System.out.println("構造器 String(int[] codePoints, int offset, int count) : "+s5);
		
		System.out.println("***************** s6 ****************");
//		String(byte bytes[], int offset, int length, String charsetName)
		try {
			FileInputStream fis = new FileInputStream("D:\\Tools\\JavaWeb\\workspace_spring\\testSE\\src\\com\\file\\FileInputStreamTest.java");
			
			byte[] bbuf = new byte[1024];
			int hasRead = 0;
			while((hasRead = fis.read(bbuf))>0){
				String s6 = new String(bbuf,0,hasRead,"utf-8");
				System.out.println(s6);
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		
		System.out.println("***************** s7 ****************");
//		String(byte bytes[], String charsetName)
		try {
			FileInputStream fis = new FileInputStream("D:\\Tools\\JavaWeb\\workspace_spring\\testSE\\src\\com\\file\\FileInputStreamTest.java");
			
			byte[] bbuf = new byte[1024];
			int hasRead = 0;
			while((hasRead = fis.read(bbuf))>0){
				String s7 = new String(bbuf,"utf-8");
				System.out.println(s7);
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		System.out.println("***************** s8 ****************");		
//		public String(byte bytes[], int offset, int length)
		try {
			FileInputStream fis = new FileInputStream("D:\\Tools\\JavaWeb\\workspace_spring\\testSE\\src\\com\\file\\FileInputStreamTest.java");
			
			byte[] bbuf = new byte[1024];
			int hasRead = 0;
			while((hasRead = fis.read(bbuf))>0){
				String s8 = new String(bbuf,0,hasRead);
				System.out.println(s8);
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		
		System.out.println("***************** s9 ****************");		
//		public String(byte bytes[])
		try {
			FileInputStream fis = new FileInputStream("D:\\Tools\\JavaWeb\\workspace_spring\\testSE\\src\\com\\file\\FileInputStreamTest.java");
			
			byte[] bbuf = new byte[1024];
			int hasRead = 0;
			while((hasRead = fis.read(bbuf))>0){
				String s9 = new String(bbuf);
				System.out.println(s9);
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}