`
jiao13953900900
  • 浏览: 32222 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

黑马程序员-IO_字符编码

    博客分类:
  • java
阅读更多

 

---------------------- android培训java培训、期待与您交流! ----------------------

 

 

字符编码

 

 

字符流的出现为了方便操作字符。

更重要是的加入了编码转换。

通过子类转换流来完成。

InputStreamReader

OutputStreamWriter

在两个对象进行构造的时候可以加入字符集

 

编码表的由来

计算机只能识别二进制数据,早期由来是电信号。

为了方便应用计算机,让它可以识别各个国家的文字。

就将各个国家的文字用数字来表示,并一一对应,形成一张表。

这就是编码表。

 

常见的编码表

ASCII:美国标准信息交换码。

用一个字节的7位可以表示。

ISO8859-1:拉丁码表。欧洲码表

用一个字节的8位表示。为避免和AscII重复,使用开头为1。后面任意排列。

tomcat服务器默认编码。

GB2312:中国的中文编码表。

两个字节表示一个汉字。为避免重复,所以两个字节的高位都是1,也就是由两个负数组成。大约六七千字

GBK:中国的中文编码表升级,融合了更多的中文文字符号。

两万多中文字符。。

Unicode:国际标准码,融合了多种文字。

所有国家的文字都用两个字节来表示,Java语言使用的就是unicode,65535个

UTF-8:最多用三个字节来表示一个字符。

在每一个开头都有标识头。使用方便。

 

转换流的编码应用

可以将字符以指定编码格式存储。

可以对文本数据指定编码格式来解读。

指定编码表的动作由构造函数完成。

 

 

字符编码

编码:字符串?字节数组

解码:字节数组?字符串

 

import java.io.*;

class EncodeStream 
{
	public static void main(String[] args) throws IOException 
	{
			//writeText();
			readText();
	}
	//按照指定的编码读取文件中 的数据
	public static void readText()throws IOException 
	{
		InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");

		char[] buf = new char[10];
		int len = isr.read(buf);

		String str = new String(buf,0,len);

		System.out.println(str);

		isr.close();
	}
	//指定编码将你好写入一个文件
	public static void writeText()throws IOException 
	{
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");

		osw.write("你好");

		osw.close();
	}

}

 

编码:字符串变成字节数组。

解码:字节数组变成字符串。

 

String-->byte[];  str.getBytes(charsetName);

byte[] -->String: new String(byte[],charsetName);

 

import java.util.*;
class  EncodeDemo
{
	public static void main(String[] args)throws Exception 
	{
		String s = "哈哈";

		byte[] b1 = s.getBytes("GBK");

		System.out.println(Arrays.toString(b1));
		String s1 = new String(b1,"utf-8");
		System.out.println("s1="+s1);

		//对s1进行iso8859-1编码。
		byte[] b2 = s1.getBytes("utf-8");
		System.out.println(Arrays.toString(b2));

		String s2 = new String(b2,"gbk");

		System.out.println("s2="+s2);

		

	}
}

 

 

class EncodeDemo2 
{
	public static void main(String[] args) throws Exception
	{
		String s = "联通";

		byte[] by = s.getBytes("gbk");

		for(byte b : by)
		{
			System.out.println(Integer.toBinaryString(b&255));
		}


		System.out.println("Hello World!");
	}
}

 

 

 

递归

函数自己调用自己。

注意:递归时一定要明确结束条件。

应用场景:

当某一功能要重复使用时。

 

列出指定目录下文件或者文件夹,包含子目录中的内容。

也就是列出指定目录下所有内容。

 

因为目录中还有目录,只要使用同一个列出目录功能的函数完成即可。

在列出过程中出现的还是目录的话,还可以再次调用本功能。

也就是函数自身调用自身。

这种表现形式,或者编程手法,称为递归。

 

递归要注意:

1,限定条件。

 

2,要注意递归的次数。尽量避免内存溢出。

 

import java.io.*;

class FileDemo3 
{
	public static void main(String[] args) 
	{
		File dir = new File("d:\\testdir");
		showDir(dir,0);

		toBin(6);
		//int n = getSum(8000);
		//System.out.println("n="+n);
	
		System.out.println(dir.delete());
	}
	public static String getLevel(int level)
	{
		StringBuilder sb = new StringBuilder();
		sb.append("|--");
		for(int x=0; x<level; x++)
		{
			//sb.append("|--");//在结尾处添加|--
			sb.insert(0,"|  ");//前面不带--
		}
		return sb.toString();
	}
	//使用递归的方式获取目录下的所有文件
	public static void showDir(File dir,int level)
	{
		
		System.out.println(getLevel(level)+dir.getName());

		level++;
		File[] files = dir.listFiles();
		//判断文件对象是否是目录,是就递归调用,不是就打印出来
		for(int x=0; x<files.length; x++)
		{
			if(files[x].isDirectory())
				showDir(files[x],level);
			else
				System.out.println(getLevel(level)+files[x]);
		}
	}
	//使用递归进行对数据求和,不可以计算过大的数据
	public static int getSum(int n)
	{
		if(n==1)
			return 1;
		return n+getSum(n-1);
	}
	//使用队规的方式计算出来二进制转换
	public static void toBin(int num)
	{
		if(num>0)
		{
			toBin(num/2);
			System.out.println(num%2);
		}
	}
	public static void method()
	{
		method();
	}
	
	

}

 

 

删除一个带内容的目录。

删除原理:

在window中,删除目录从里面往外删除的。

 

既然是从里往外删除。就需要用到递归。

 

import java.io.*;
class  RemoveDir
{
	public static void main(String[] args) 
	{
		
		File dir = new File("d:\\testdir");
		removeDir(dir);
	}
	//递归式的删除文件对象
	public static void removeDir(File dir)
	{
		File[] files = dir.listFiles();
		for(int x=0; x<files.length; x++)
		{
			if(files[x].isDirectory())
				removeDir(files[x]);
			else
				System.out.println(files[x].toString()+":-file-:"+files[x].delete());
		}

		System.out.println(dir+"::dir::"+dir.delete());
	}

}

 

 

 

 

---------------------- android培训java培训、期待与您交流! ----------------------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics