经常忘记或者混淆的一些常用
2016-12-14 15:56
316 查看
1k = 1024byte(字节)
1byte(字节) = 8位(bit)
UTF-8编码格式中一个汉字用的三个字节,有的用的4个字节保存
GBK编码各种中一个汉字用的是2个字节保存,
所以用不同的编码格式 就会出现汉字为乱码的情况
字符就是几个字母,比如a和A都是一个字符.aa就是两个字符.
每个字节是由8位组成的.位是最小的单位了,叫做bit.
而字节呢,叫做byte.
所以,一个英文字母,无论大写和小写都是一个字符、一个字节,8位.
一个汉字是一个字符、两个字节,16位
字节流 和 字符楼
通过查在线文档:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh 在线jdk文档得知:
字节流中:FileInputStream 构造函数为:FileInputStream(File file)
BufferedInputStream 构造函数:BufferedInputStream(InputStream in) 或者 BufferedInputStream(InputStream in, int size)
DataInputStreamg构造函数:DataInputStream(InputStream in)
StringBufferInputStream 构造函数:StringBufferInputStream(String s)
ByteArrayInputStream 构造函数:ByteArrayInputStream(byte[] buf) 或者 ByteArrayInputStream(byte[] buf, int offset, int length)创建
一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组
字符流中:BufferedReader构造函数:BufferedReader(Reader in)
FileReader构造函数:FileReader(File file)
StringReader构造函数:
ByteArrayReader构造函数:
IO流的分类
下面内容参考文章:http://www.2cto.com/kf/201312/262036.html 如若冒犯原作者的地方可联系本人删除即可。
根据处理数据类型的不同分为:字符流和字节流
根据数据流向不同分为:输入流和输出流
InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。PipedInputStream 是从与其它线程共用的管道中读取数据,与Piped 相关的知识后续单独介绍。
ObjectInputStream 和所有FilterInputStream的子类都是装饰流(装饰器模式的主角)。
eg:
/**
* 字节流
* 读文件内容,节省空间
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
in.read(b);
System.out.println("文件长度为:"+f.length());
in.close();
System.out.println(new String(b));
}
}
逐字节读:
/**
* 字节流
* 读文件内容,节省空间
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
for (int i = 0; i < b.length; i++) {
b[i]=(byte)in.read();
}
in.close();
System.out.println(new String(b));
}
}
DataInputStream:注意参数是inputstream 不是file
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class DataOutputStreamDemo{
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator +"hello.txt");
DataInputStream input = new DataInputStream(new FileInputStream(file));
char[] ch = new char[10];
int count = 0;
char temp;
while((temp = input.readChar()) != 'C'){
ch[count++] = temp;
}
System.out.println(ch);
}
}
OutputStream 是所有的输出字节流的父类,它是一个抽象类。
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。PipedOutputStream 是向与其它线程共用的管道中写入数据,
ObjectOutputStream 和所有FilterOutputStream的子类都是装饰流。
eg:向文件中写入字符串
/**
* 字节流
* 向文件中写入字符串
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f);
String str="Hello World";
byte[] b=str.getBytes();
out.write(b);
out.close();
}
}
逐字节写入文件:
/**
* 字节流
* 向文件中一个字节一个字节的写入字符串
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f);
String str="Hello World!!";
byte[] b=str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
}
向文件中追加新内容
/**
* 字节流
* 向文件中追加新内容:
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖
String str="Rollen";
//String str="\r\nRollen"; 可以换行
byte[] b=str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
}
使用内存操作流将一个大写字母转化为小写字母:
/**
* 使用内存操作流将一个大写字母转化为小写字母
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String str="ROLLENHOLT";
ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream output=new ByteArrayOutputStream();
int temp=0;
while((temp=input.read())!=-1){
char ch=(char)temp;
output.write(Character.toLowerCase(ch));
}
String outStr=output.toString();
input.close();
output.close();
System.out.println(outStr);
}
}
DataOutputStream类示例:
import
java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo{
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator +"hello.txt");
char[] ch = { 'A', 'B', 'C' };
DataOutputStream out = null;
out = new DataOutputStream(new FileOutputStream(file));
for(char temp : ch){
out.writeChar(temp);
}
out.close();
}
}
Reader:
InputStreamReader:字节到字符的桥梁
OutputStreamWriter:字符到字节的桥梁
Reader
是所有的输入字符流的父类,它是一个抽象类。
CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。PipedReader 是从与其它线程共用的管道中读取数据。
BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。
InputStreamReader
是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。
eg:
/**
* 字符流
* 从文件中读出内容
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
char[] ch=new char[100];
Reader read=new FileReader(f);
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为"+new String(ch,0,count));
}
}
writer:
Writer
是所有的输出字符流的父类,它是一个抽象类。
CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。
PipedWriter 是向与其它线程共用的管道中写入数据,
BufferedWriter 是一个装饰器为Writer 提供缓冲功能。
向文件中写入数据:
eg:
/**
* 字符流
* 写入数据
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
Writer out =new FileWriter(f);
String str="hello";
out.write(str);
out.close();
}
}
将字节输出流转化为字符输出流:
eg:/**
* 将字节输出流转化为字符输出流
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Writer out=new OutputStreamWriter(new FileOutputStream(file));
out.write("hello");
out.close();
}
}
将字节输入流转换为字符输入流:
/**
* 将字节输入流变为字符输入流
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Reader read=new InputStreamReader(new FileInputStream(file));
char[] b=new char[100];
int len=read.read(b);
System.out.println(new String(b,0,len));
read.close();
}
}
1byte(字节) = 8位(bit)
UTF-8编码格式中一个汉字用的三个字节,有的用的4个字节保存
GBK编码各种中一个汉字用的是2个字节保存,
所以用不同的编码格式 就会出现汉字为乱码的情况
字符就是几个字母,比如a和A都是一个字符.aa就是两个字符.
每个字节是由8位组成的.位是最小的单位了,叫做bit.
而字节呢,叫做byte.
所以,一个英文字母,无论大写和小写都是一个字符、一个字节,8位.
一个汉字是一个字符、两个字节,16位
字节流 和 字符楼
通过查在线文档:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh 在线jdk文档得知:
字节流中:FileInputStream 构造函数为:FileInputStream(File file)
BufferedInputStream 构造函数:BufferedInputStream(InputStream in) 或者 BufferedInputStream(InputStream in, int size)
DataInputStreamg构造函数:DataInputStream(InputStream in)
StringBufferInputStream 构造函数:StringBufferInputStream(String s)
ByteArrayInputStream 构造函数:ByteArrayInputStream(byte[] buf) 或者 ByteArrayInputStream(byte[] buf, int offset, int length)创建
一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组
字符流中:BufferedReader构造函数:BufferedReader(Reader in)
FileReader构造函数:FileReader(File file)
StringReader构造函数:
StringReader(String s)
ByteArrayReader构造函数:
IO流的分类
下面内容参考文章:http://www.2cto.com/kf/201312/262036.html 如若冒犯原作者的地方可联系本人删除即可。
根据处理数据类型的不同分为:字符流和字节流
根据数据流向不同分为:输入流和输出流
InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。PipedInputStream 是从与其它线程共用的管道中读取数据,与Piped 相关的知识后续单独介绍。
ObjectInputStream 和所有FilterInputStream的子类都是装饰流(装饰器模式的主角)。
eg:
/**
* 字节流
* 读文件内容,节省空间
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
in.read(b);
System.out.println("文件长度为:"+f.length());
in.close();
System.out.println(new String(b));
}
}
逐字节读:
/**
* 字节流
* 读文件内容,节省空间
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
for (int i = 0; i < b.length; i++) {
b[i]=(byte)in.read();
}
in.close();
System.out.println(new String(b));
}
}
DataInputStream:注意参数是inputstream 不是file
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class DataOutputStreamDemo{
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator +"hello.txt");
DataInputStream input = new DataInputStream(new FileInputStream(file));
char[] ch = new char[10];
int count = 0;
char temp;
while((temp = input.readChar()) != 'C'){
ch[count++] = temp;
}
System.out.println(ch);
}
}
OutputStream 是所有的输出字节流的父类,它是一个抽象类。
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。PipedOutputStream 是向与其它线程共用的管道中写入数据,
ObjectOutputStream 和所有FilterOutputStream的子类都是装饰流。
eg:向文件中写入字符串
/**
* 字节流
* 向文件中写入字符串
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f);
String str="Hello World";
byte[] b=str.getBytes();
out.write(b);
out.close();
}
}
逐字节写入文件:
/**
* 字节流
* 向文件中一个字节一个字节的写入字符串
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f);
String str="Hello World!!";
byte[] b=str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
}
向文件中追加新内容
/**
* 字节流
* 向文件中追加新内容:
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖
String str="Rollen";
//String str="\r\nRollen"; 可以换行
byte[] b=str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
}
使用内存操作流将一个大写字母转化为小写字母:
/**
* 使用内存操作流将一个大写字母转化为小写字母
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String str="ROLLENHOLT";
ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream output=new ByteArrayOutputStream();
int temp=0;
while((temp=input.read())!=-1){
char ch=(char)temp;
output.write(Character.toLowerCase(ch));
}
String outStr=output.toString();
input.close();
output.close();
System.out.println(outStr);
}
}
DataOutputStream类示例:
import
java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo{
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator +"hello.txt");
char[] ch = { 'A', 'B', 'C' };
DataOutputStream out = null;
out = new DataOutputStream(new FileOutputStream(file));
for(char temp : ch){
out.writeChar(temp);
}
out.close();
}
}
Reader:
InputStreamReader:字节到字符的桥梁
OutputStreamWriter:字符到字节的桥梁
Reader
是所有的输入字符流的父类,它是一个抽象类。
CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。PipedReader 是从与其它线程共用的管道中读取数据。
BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。
InputStreamReader
是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。
eg:
/**
* 字符流
* 从文件中读出内容
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
char[] ch=new char[100];
Reader read=new FileReader(f);
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为"+new String(ch,0,count));
}
}
writer:
Writer
是所有的输出字符流的父类,它是一个抽象类。
CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。
PipedWriter 是向与其它线程共用的管道中写入数据,
BufferedWriter 是一个装饰器为Writer 提供缓冲功能。
向文件中写入数据:
eg:
/**
* 字符流
* 写入数据
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
Writer out =new FileWriter(f);
String str="hello";
out.write(str);
out.close();
}
}
将字节输出流转化为字符输出流:
eg:/**
* 将字节输出流转化为字符输出流
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Writer out=new OutputStreamWriter(new FileOutputStream(file));
out.write("hello");
out.close();
}
}
将字节输入流转换为字符输入流:
/**
* 将字节输入流变为字符输入流
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "d:"+File.separator+"hello.txt";
File file=new File(fileName);
Reader read=new InputStreamReader(new FileInputStream(file));
char[] b=new char[100];
int len=read.read(b);
System.out.println(new String(b,0,len));
read.close();
}
}
相关文章推荐
- HTML常用的特殊符号总结 2014年9月12日 22704次浏览 html中经常会用到一些特殊符号,例如箭头,雪花,心形等等,这些符号就不用css样式或者图片来写了,直接用html特殊符号可以实现。
- eclipse的一些常用的单经常忘记的操作
- Android学习笔记-常用的一些源码,防止忘记了。。。
- 黑马程序员——DOM编程中一些容易忘记的常用属性跟方法的应用。
- 常用的运行命令,我经常忘记的----个人笔记
- 【汇编】一些常用但又很容易忘记的汇编指令
- Android学习笔记-常用的一些源码,防止忘记了。。。
- 我经常忘记的一些小技巧
- GUI常用到的一些网站或者资料
- Android学习笔记-常用的一些源码,防止忘记了。。。
- 经常忘记的一些进程
- JSP中一些需要注意和经常忘记的问题
- IIS版面中经常看到大家问一些相同的或者以前已经多次解答的问题。所以,我将这些问题整理了一些出来供大家参考。
- String类一些常用及容易混淆的操作
- C#中一些没搞明白或者容易混淆的概念以及面试中可能遇到的问题
- vim常用命令总结;方便自己查看,大家可以自己开个帖子,把自己常用的,或者经常忘的贴在上面方便自己的查找(不断补充中)
- 经常用到经纬度数据,一些常用的经纬度经常记不住,索性一次性做个整理~~
- Android学习笔记-常用的一些源码,防止忘记了。。。
- 一些常用又容易忘记的数据库总结
- 一些前端真正常用的工具和网站(会经常更新)