JAVA学习日记06

来源:互联网 发布:php代码混淆解密工具 编辑:程序博客网 时间:2024/05/17 08:56

1503-3-吴天明  总结《2016年-10月-06日》【连续 6天总结】

关键词Java 流(Stream)文件(File)和IO、Java Scanner 类、Java 异常处理

内容

A. 概括   

(a)Java 流(Stream)文件(File)和IO

(b)Java Scanner 类

(c)Java 异常处理

B.具体内容

        今天算是弄懂了之前一直写的System.out.println( );是什么意思了;与C++中类似,读取输入流中的字符和将字符放入输出流中都是通过类对象来完成的,只不过java的类方法肯更具体,C++的输入输出方法更加通用。其次,异常捕获也是开始学java以后才了解的,学C++时并没有学到,可能是学的不全面,不够透彻。

C.明日计划(需要量化的目标)

(a)Java 继承

(b)Java 重写(Override)与重载(Overload)

(c)Java 多态

(d)Java 抽象类

今日代码:

输入输出流:

import java.io.*;public class MyIO {public static void main(String[] args) throws IOException{BufferedReader in=new BufferedReader(new InputStreamReader(System.in));char a;//使用System。in创建BufferedReader对象System.out.println("请输入字符(\"q\"to quit):");//调用read()方法逐个读取字符do{a=(char)in.read();//使用write(int)方法进行输出System.out.write(a);}while(a!='q');System.out.write('\n');        String str;        System.out.println("请输入一行字符串(以end为停止标记):");        do{        str=in.readLine();        System.out.println(str);        }while(!str.equals("end"));                char[] array={'a','b','c','d','e','f','g'};        //text.txt文件自动创建在project目录下        OutputStream outfile=new FileOutputStream("text.txt");        for(char x:array){        outfile.write(x);        }        //关闭输出流        outfile.close();        InputStream infile=new FileInputStream("text.txt");        int size=infile.available();        for(int i=0;i<size;i++){        System.out.print((char)infile.read()+" ");        }        infile.close();}}
运行结果:

请输入字符("q"to quit):werqthis iswerq请输入一行字符串(以end为停止标记):this isthis is a line endthis is a line end
Scanner类:
import java.util.Scanner;public class MyScanner {public static void main(String[] args) {Scanner scan=new Scanner(System.in);System.out.println("采用next方式接收:");        //读取之前首先要判断是否还有输入if(scan.hasNext()){String str1=scan.next();System.out.println("输入的数据为:"+str1);}System.out.println("采用nextline方式接收");if(scan.hasNextLine()){String str2=scan.nextLine();System.out.println("输入的行为:"+str2);}int intnumber=0;if(scan.hasNextInt()){intnumber=scan.nextInt();System.out.println("输入整数为:"+intnumber);}scan.close();}}
运行结果:

采用next方式接收:qwer qwe qw q 6输入的数据为:qwer采用nextline方式接收输入的行为: qwe qw q 66输入整数为:6




0 0
原创粉丝点击