PrintStream和PrintWriter
/*
* 打印流:输出流
* (1)PrintStream:
* 经典代表:System.out
* Sysetm.err
*
* new PrintStream(文件名)
* new PrintStream(文件名,编码)
* new PrintStream(另一个字节输出流)
*
* (2)PrintWriter
* Web阶段学习时,从服务器端往客户端返回消息时用到response,response.getWriter()可以返回PrintWriter对象。
* 即 Web服务器往客户端(例如:浏览器)返回html网页时,用的是PrintWriter对象的输出方法。
*
*
*/
public class TestPrintStream {
@Test
public void test04() throws IOException{
//所有数据类型写出去,都是按照文本处理
PrintStream ps = new PrintStream("1.txt");
ps.println(12);
ps.println(true);
ps.println(new Goods("《从入门到放弃》", 99.99, 1000));
ps.close();
}
@Test
public void test03() throws IOException{
PrintStream ps = new PrintStream("d:/1.txt","GBK");
ps.println("中文");
ps.close();
}
@Test
public void test02() throws IOException{
PrintStream ps = new PrintStream("1.txt");
ps.println("hello");
ps.println("world");
ps.close();
}
@Test
public void test01(){
PrintStream out = System.out;
out.println("hello");
// out.print();
out.println();
}
}
文本扫描仪:scanner
/*
* System.in:默认情况下是从键盘输入的数据中扫描
* Scanner:可以从你指定的文件、流中读取文本数据
*/
public class TestScanner {
@Test
public void test05() throws FileNotFoundException{
Scanner input = new Scanner(new File("d:/1.txt"),"GBK");//InputStream
while(input.hasNextLine()){
String line = input.nextLine();
System.out.println(line);
}
input.close();
}
@Test
public void test04() throws FileNotFoundException{
Scanner input = new Scanner("1.txt");//InputStream
while(input.hasNextLine()){
String line = input.nextLine();
System.out.println(line);
}
input.close();
}
@Test
public void test03() throws FileNotFoundException{
Scanner input = new Scanner(new File("1.txt"));//InputStream
while(input.hasNextLine()){
String line = input.nextLine();
System.out.println(line);
}
input.close();
}
@Test
public void test02() throws FileNotFoundException{
Scanner input = new Scanner(new FileInputStream("1.txt"));//InputStream
while(input.hasNextLine()){
String line = input.nextLine();
System.out.println(line);
}
input.close();
}
@Test
public void test01(){
Scanner input = new Scanner(System.in);
System.out.print("请输入一个整数:");
int num = input.nextInt();
System.out.println("num = " + num);
input.close();
}
}
System类中的IO流
/*
* 在Java层面是常量对象,但是可以同C等底层语言进行修改
* System.in:
* System.out
* System.err
*/
public class TestSystem {
@Test
public void test02() throws FileNotFoundException{
System.setOut(new PrintStream("1.txt"));
System.out.println("aaaa");
System.out.println("bbb");
System.out.println("ccc");
System.out.println("ddd");
}
@Test
public void test01(){
PrintStream out = System.out;
System.out.println(out);
}
}
JDK1.7新增的Try catch处理方式
/*
* JDK1.7中新增了一种try...catch处理的方式,
* 称为try...with....resource,它是为资源关闭专门设计的try...catch的语法
*
* try(
* 需要关闭的资源对象
* ){
* 可能发生异常的逻辑代码
* }catch(异常类型 e){
* 异常处理代码
* }catch(异常类型 e){
* 异常处理代码
* }
*
* 凡是在try()中声明的资源对象,都会自动关闭,无论是否发生异常。
*/
public class TestTryWithResource {
@Test
public void test03() {
//从d:/1.txt(GBK)文件中,读取内容,写到项目根目录下1.txt(UTF-8)文件中
try(
FileInputStream fis = new FileInputStream("d:/1.txt");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream("1.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
){
String str;
while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test02() {
//从d:/1.txt(GBK)文件中,读取内容,写到项目根目录下1.txt(UTF-8)文件中
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/1.txt"),"GBK"));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("1.txt"),"UTF-8"));
String str;
while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bw!=null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(br!=null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test01(){
//从d:/1.txt(GBK)文件中,读取内容,写到项目根目录下1.txt(UTF-8)文件中
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
fis = new FileInputStream("d:/1.txt");
isr = new InputStreamReader(fis,"GBK");
br = new BufferedReader(isr);
fos = new FileOutputStream("1.txt");
osw = new OutputStreamWriter(fos,"UTF-8");
bw = new BufferedWriter(osw);
String str;
while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bw!=null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(osw!=null){
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(br!=null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(isr!=null){
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* public static void main(String[] args) {
//从d:/1.txt(GBK)文件中,读取内容,写到项目根目录下1.txt(UTF-8)文件中
FileInputStream fis = new FileInputStream("d:/1.txt");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream("1.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
String str;
while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
}
bw.close();
osw.close();
fos.close();
br.close();
isr.close();
fis.close();
}*/
}