您的位置:首页 > 编程语言 > Java开发

java第二阶段day1

2015-12-16 00:00 344 查看
-----------------------------------------------------
unicode: 统一编码 一个字母和一个汉字都是占2个字节

GBK:中国的汉字编码,GB2312 ,GBK的范围比GBK2312更广
一个字母占1个字节 一个汉字占2个字节

UTF-8: 一个字母占1一个字节 一个汉字占 3个字节
----------------------------------------------------

io 体系
1、io -- > input:读取 output:写入
2、io 作用 -- 读取数据和写入数据

io 体系的类都包含在 java.io 包
3、io分类
1 按数据方向来分类 读取流(Input,reader)和写入流(Outerput,writer)
2 按数据的类型分类 字节流(以Stream结尾)和字符流(以reader,writer结尾)
字符流是在字节流的基础上家里编码机制

字节流 InputStream OutputStream

public class InputStream1 {
public static void main(String[] args) throws Exception {
InputStream input = new FileInputStream("d:\\aa.txt");//指定路径

byte[] b = new byte[4];

//int length = 0;
while((length = input.read(b)) != -1){//第一次存4个
//第二次重新赋值(覆盖)
System.out.println(new String(b,0,b.length));
}

}

}

------------------
public class OutPutStream1 {

public static void main(String[] args) throws Exception{
OutputStream output = new FileOutputStream("D:\\1510.txt");

Scanner sc = new Scanner(System.in);
while(true){
String str = sc.next();
if("886".equals(str)){break;}
output.write(str.getBytes());
output.write("\r\n".getBytes());//回车换行
}

}

}

----------------

字符流 BufferedReader BufferedWriter

public class BufferRead {

public static void main(String[] args) throws Exception {
Reader reader = new FileReader("d:\\copy.txt");
BufferedReader bf = new BufferedReader(reader);
String str;
//readLine() 读取整行内容
while((str = bf.readLine()) != null){
System.out.println(str);
}

}

}
-----------------

//接收键盘录入的多行数据,将它写到文件中。BufferedWriter,需要条件。
public class BufferedWriterDemo1 {
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new FileWriter("f:\\aaa.txt"));
Scanner input = new Scanner(System.in);
String str = null;
while(true){
str = input.nextLine();
if("886".equals(str)){
break;
}
//写数据
bw.write(str);
//写入换行符。
bw.newLine();//根据平台的不同,写入的换行符也会不一样。
bw.flush();
}
bw.close();

}

}
---------------

缓冲流 BufferedInputStream BufferedOuterputStream

//效率上。用法跟InputStream是一样的。内部有一个内置的缓冲区。
public class BufferedOutputStream1 {

public static void main(String[] args)throws Exception {
BufferedOutputStream bs = new BufferedOutputStream(new FileOutputStream
("d:\\d.txt"));

byte[] b = new byte[1024];
Scanner sc = new Scanner(System.in);
while(true){
String str = sc.next();
if("1".equals(str)){break;}

b = str.getBytes();

bs.write(b);
bs.flush();
}
bs.close();
}

}
-------------
单例模式

/*单例模式
* 1:构造方法私有化。
*/
public class SingInstance {
private SingInstance(){

}
private static SingInstance single;//懒汉式。在声明的时候没有给它创建对象。
public static SingInstance getInstance(){//线程不安全。讲线程的时候再解决问题。
if(single==null){
single = new SingInstance();
}
return single;
}
}
------------

/*单例模式
* 1:构造方法私有化。
* 2:声明一个静态的成员变量。
* 3:提供一个公共的方法,返回该类的实例。
* 饿汉式跟懒汉式的区别
* 1:项目中用饿汉式。而面试经常问懒汉式。
* 2:懒汉式代码是有缺陷的,会出现问题。
*/
public class SingInstance2 {
private SingInstance2() {

}

private static SingInstance2 single = new SingInstance2();// 饿汉式。

public static SingInstance2 getInstance() {
return single;
}
}
---------------------------------------------------------------------------------------------------------------------

public class CopyMethod {
// private String rpath; //读取路径
// private String wpath; //写入的路径

//字节流
public void method(String rp,String wp) throws IOException{
OutputStream os = new FileOutputStream(wp);
InputStream is = new FileInputStream(rp);

int l = is.available();
//读取文件到数组B里
byte[] b = new byte[l];
int length = 0;
while((length = is.read(b,0,l)) != -1){
//把数组B中的数据写入文件
os.write(b);
}
os.close();
is.close();
}

//字符流
public void method2(String rp,String wp) throws IOException{
Reader r = new FileReader(rp);
Writer w = new FileWriter(wp);
BufferedReader br = new BufferedReader(r);
BufferedWriter bw = new BufferedWriter(w);

String str;
//读取数据
while((str = br.readLine()) != null){
//写入数据
bw.write(str);
bw.flush();
}

bw.close();
br.close();
}

//缓冲流
public void method3(String rp,String wp) throws IOException{
OutputStream os = new FileOutputStream(wp);
InputStream is = new FileInputStream(rp);
BufferedOutputStream bos = new BufferedOutputStream(os);
BufferedInputStream bis = new BufferedInputStream(is);
int l = bis.available();
byte[] b = new byte[l];
int length = 0;
//读取
while((length = bis.read(b,0,l)) != -1){
//写入
bos.write(b,0,length);
bos.flush();
}
bos.close();
bis.close();
}

public static void main(String[] args) throws IOException {
CopyMethod copy = new CopyMethod();
copy.method("D:\\mp3\\许嵩 - 认错.mp3","D:\\ - 认错.mp3");
//copy.method2("D:\\mp3\\许嵩 - 认错.mp3","D:\\许 - 认错.mp3");
//copy.method3("D:\\mp3\\许嵩 - 千百度.mp3","D:\\许- 千度.mp3");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息