黑马程序员22-1:控制台IO,输入输出流转换,输入输出到文件,字体

[align=center]------- [url=http://edu.csdn.net/heima]android培训 [/url]、[url=http://edu.csdn.net/heima]java培训[/url]、期待与您交流!------- [/align]



package cn.itcast.io.p1.transstream.demo;

import java.io.IOException;
import java.io.InputStream;
/*
* 读取一个键盘录入的数据,并打印在控制台上。
*
* 键盘本身就是一个标准的输入设备。
* 对于java而言,对于这种输入设备都有对应的对象。
*
*
*/
public class ReadKey {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

// readKey();
// System.out.println((int)'\r');
// System.out.println((int)'\n');

readKey2();

}

public static void readKey2() throws IOException {

/*
* 获取用户键盘录入的数据,
* 并将数据变成大写显示在控制台上,
* 如果用户输入的是over,结束键盘录入。
*
* 思路:
* 1,因为键盘录入只读取一个字节,要判断是否是over,需要将读取到的字节拼成字符串。
* 2,那就需要一个容器。StringBuilder.
* 3,在用户回车之前将录入的数据变成字符串判断即可。
*
*/

//1,创建容器。
StringBuilder sb = new StringBuilder();

//2,获取键盘读取流。
InputStream in = System.in;

//3,定义变量记录读取到的字节,并循环获取。
int ch = 0;

while((ch=in.read())!=-1){

// 在存储之前需要判断是否是换行标记 ,因为换行标记不存储。
if(ch=='\r')
continue;
if(ch=='\n'){
String temp = sb.toString();
//自定义判断标记,当只输入over时关闭
if("over".equals(temp))
break;
System.out.println(temp.toUpperCase());
//输出之后,记得删除已经存进去的东西,重新存
sb.delete(0, sb.length());
}
else
//将读取到的字节存储到StringBuilder中。
sb.append((char)ch);

// System.out.println(ch);
}

}

public static void readKey() throws IOException {

InputStream in = System.in;

int ch = in.read();//阻塞式方法。
System.out.println(ch);
int ch1 = in.read();//阻塞式方法。
System.out.println(ch1);
int ch2 = in.read();//阻塞式方法。
System.out.println(ch2);

// in.close();

// InputStream in2 = System.in;
// int ch3 = in2.read();

}


}




package cn.itcast.io.p1.transstream.demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class TransStreamDemo {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

//字节流。
InputStream in = System.in;
// int ch = in.read();
// System.out.println(ch);
// int ch1 = in.read();
// System.out.println(ch1);

//将字节转成字符的桥梁。转换流。
InputStreamReader isr = new InputStreamReader(in);

// int ch = isr.read();
// System.out.println((char)ch);

//字符流。
BufferedReader bufr = new BufferedReader(isr);

OutputStream out = System.out;

OutputStreamWriter osw = new OutputStreamWriter(out);

BufferedWriter bufw = new BufferedWriter(osw);



String line = null;

while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
// System.out.println(line.toUpperCase());
// osw.write(line.toUpperCase()+"\r\n");
// osw.flush();

bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}


}

}



package cn.itcast.io.p1.transstream.demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TransStreamDemo2 {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {


/*
* 1,需求:将键盘录入的数据写入到一个文件中。
*
* 2,需求:将一个文本文件内容显示在控制台上。
*
* 3,需求:将一个文件文件中的内容复制到的另一个文件中。
*/

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b.txt")));


String line = null;

while((line=bufr.readLine())!=null){
if("over".equals(line))
break;

bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}


}

}



package cn.itcast.io.p1.transstream.demo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class TransStreamDemo3 {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

readText_2();
}

public static void readText_2() throws IOException, FileNotFoundException {


InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk_1.txt"),"utf-8");
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 readText_1() throws IOException {

FileReader fr = new FileReader("gbk_1.txt");

char[] buf = new char[10];
int len = fr.read(buf);
String str = new String(buf,0,len);
System.out.println(str);

fr.close();


}

public static void writeText_3() throws IOException {

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"),"UTF-8");

osw.write("你好");
osw.close();

}

public static void writeText_2() throws IOException {

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"),"GBK");

// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"),"GBK");
// FileWriter fw = new FileWriter("gbk_1.txt");

/*
* 这两句代码的功能是等同的。
* FileWriter:其实就是转换流指定了本机默认码表的体现。而且这个转换流的子类对象,可以方便操作文本文件。
* 简单说:操作文件的字节流+本机默认的编码表。
* 这是按照默认码表来操作文件的便捷类。
*
* 如果操作文本文件需要明确具体的编码。FileWriter就不行了。必须用转换流。
*
*/


osw.write("你好");

osw.close();


}

public static void writeText_1() throws IOException {

FileWriter fw = new FileWriter("gbk_1.txt");

fw.write("你好");

fw.close();
}

}




[align=center]------- [url=http://edu.csdn.net/heima]android培训 [/url]、[url=http://edu.csdn.net/heima]java培训[/url]、期待与您交流!------- [/align]

[align=center]详细请查看:[url=http://edu.csdn.net/heima]http://edu.csdn.net/heima [/url]------- [/align]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值