学习心得
一、专业课
1、随机流
* 随机流:不属于io的成员
*1.可以直接操作数据
*2.文件指针的特性,结合多线程复制一个文件
Filefile= newFile("d:/d10.txt");
//权限有四种模式
RandomAccessFileraf = new RandomAccessFile(file, "rw");
raf.seek(0);
raf.writeUTF("d迪d");
System.out.println(raf.getFilePointer());//文件指针
raf.writeInt(2);
raf.writeChar('B');
raf.seek(0);//绝对定位,改变文件指针
raf.skipBytes(7);//相对定位
System.out.println(raf.readInt());
2.配置文件
/**
* 配置文件的使用
* 一些项目常用的固定参数信息,通常会键一个文件进行 存储
* 1.存放的数据信息是键值对
* 2.Properties存放的键值对都是字符串
*/
Propertiesproperties = new Properties();
//获取配置文件流
InputStreaminputStream =Demo03.class.getClassLoader().getResourceAsStream("test.properties");
InputStreamReaderreader = new InputStreamReader(inputStream);
//加载文件流将流里面的配置信息加载进Pro的列表
properties.load(inputStream);
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("age"));
2.1向配置文件写东西
//往配置文件中写东西
Propertiesproperties = new Properties();
FileOutputStreamfos = new FileOutputStream("test.properties");
properties.setProperty("dd","dd");
properties.store(fos,"我的注释");
fos.flush();
fos.close();
3装饰者设计模式
1.包含要争强类的引用
//提升功能的类装饰类
classDBuffer extends BufferedReader {
intnum;
publicBufferedReader bReader; // 1.包含要增强类的对象引用
publicDBuffer(BufferedReader in) {
super(in);
//TODO Auto-generated constructor stub
this.bReader= in;
}
publicString readLine() throws IOException {
//TODO Auto-generated method stub
Stringstring = null;
try{
//string = super.readLine();
string= bReader.readLine(); // 对增强类对象的方法 扩展
if(null != string) {
return(++num) + " : " + string;
}
}catch (Exception e) {
//TODO: handle exception
}
returnstring;
}
}
//使用装饰者模式
DBufferreader = new DBuffer(
newBufferedReader(new FileReader("d:/d12.txt")) );
DBuffer2fBuffer2 = new DBuffer2(reader);
Stringstring = null;
intnum = 0;
while(null != (string = fBuffer2.readLine())) {
System.out.println(string);
}
4.小组PK
2.1我方题目
1.阅读以下代码,若能运行输出运行结果,若不能运行说明原因:
以下程序能运行吗,可以的话请输出运行结果,如果不行,请说明原因
publicclass Demo06 {
publicstatic void main(String[] args) throws Exception {
method1();
}
privatestatic void method1() throws IOException {
DataOutputStreamdou = new DataOutputStream(new FileOutputStream(newFile("D:"+File.separator+"data.txt")));
dou.write(127);
dou.writeUTF("深");
dou.flush();
dou.close();
Stack<String>stk = new Stack<>();
stk.push("A");
stk.push("B");
stk.push("C");
stk.push("D");
stk.push("E");
stk.push("G");
stk.push("F");
stk.push("H");
stk.push("I");
for(inti=dou.size();i<stk.size();i++){
System.out.println(stk.pop());
}
}
}
输出
I
H
解析:1.dou流关闭后,后面不能再写入数据,但是可以访问它的size()方 法读取长度
2.dou数据流write方法写入文件一次写一个字节,writeUTF一次写入 汉字是3个字节,方法本身
系统定义会写入两个字节,所以相当于写了5个字节,所以从6开始弹栈, 每次弹出一个数据栈的
长度会变短,只弹出过两次(6-9)(7-8)。(8-7)的时候跳出循环
2.写出下列程序输出结果。
写出下列程序输出结果。
publicstatic void main(String[] args) {
List<String>list = new ArrayList<String>();
Stringstr = "!yae"+new String("黑d色s能b量");
Stringstr1 = "!yae黑d色s能b量";
inti = 0;
for(;i< str.length()/2;i++){
list.add(i+1+""+str.charAt(i));
}
System.out.println(str== str1);
System.out.println(str.equals(str1));
System.out.println(list.size());
try{
for(i= 0;i < 7;i++);{
list.add(i-1, "在");
}
}catch(NullPointerException e){
System.out.println("黑色能量队向你抛出空指针异常");
}catch(IndexOutOfBoundsException e){
System.out.println("黑色能量队向你抛出文件异常");
}catch(Exception e){
System.out.println("黑色能量队向你抛出异常");
}
System.out.println(list);
}
false
true
5
黑色能量队向你抛出文件异常
[1!,2y, 3a, 4e, 5黑]
3、若代码有错,请改错并写出正确结果;若代码正确,请写出正确结果。
publicclass Test2 {
publicstatic void main(String[] args) throws IOException {
Filefile = new File("D:"+File.separator+"test2.txt");
OutputStreamWriterouw = new OutputStreamWriter(new OutputStream(file),"gbk");
Stringstr = "我在深圳,helloworld !";//标点均为中文 标点
ouw.write(str.toCharArray());
ouw.flush();
ouw.close();
System.out.println(file.size());
InputStreamReaderins = new InputStreamReader(new InputStream(file),"gbk");
char[]chars = new char[1024];
intlen = ins.read(chars);
System.out.println(len);//26
Filefile1 = new File("D:"+File.separator+"test3.txt");
OutputStreamWriterouw1 = new OutputStreamWriter(new OutputStream(file1),"utf-8");
Stringstr1 = "我在深圳,helloworld !";//标点均为中文 标点
ouw1.write(str1.toCharArray());
ouw1.flush();
ouw1.close();
System.out.println(file1.size());//34
System.out.println(str.charAt(len- 4));//22
}
}
答案:
程序改错:
OutputStreamWriterouw = new OutputStreamWriter(new OutputStream(file),"gbk");
InputStreamReaderins = new InputStreamReader(new InputStream(file),"gbk");
OutputStreamWriterouw1 = new OutputStreamWriter(new OutputStream(file1),"utf-8");
OutputStream和InputStream是抽象类,不能new对象。
file.size()没有这个方法,应该是length()
运行结果:
24
18
30
l
4.publicclass Test01 {
publicstatic void main(String[] args) throws IOException {
fun1();
InputStreamReaderisr = new InputStreamReader(System.in,"gbk");
BufferedReaderbReader = new BufferedReader(isr);
char[]cbuf = new char[1024];
intlen = 0;
len= bReader.read(cbuf);
System.out.println(len+ " " + new String(cbuf,0,len));
}
privatestatic void fun1() throws IOException {
InputStreamis = System.in;
PrintStreamps = System.out;
InputStreamReaderisr = new InputStreamReader(is);
char[]cbuf = new char[1024];
intlen = 0;
do{
len= isr.read(cbuf);
ps.println(len+" "+new String(cbuf,0,len));
}while (len<2);
ps.println(len+" "+new String(cbuf,0,len));
}
}
请注意程序输出格式
程序从控制台输入1回车后程序输出什么?
3 1 //当输入1回车时缓冲区中的字符是这个: 1\r\n
3 1
程序从控制台输入12回车后程序输出什么?
412
5.填空
publicclass Test2 {
publicstatic void main(String[] args) throws IOException {
//文件路径确认没错
FileReaderfr = new FileReader(new File("E:\\test.txt"));
BufferedReaderbr = new BufferedReader(fr);
intlen = 0;
char[] ch = new char[1024];
Stringstring = "";
while((string+= br.readLine() ).getBytes().length < 50 ){
Stringstr1 = "qiao qiao gao su ni";
string= string.replaceAll("\\d", "哈");
str1+= "zhexie dou shi mei yong";
System.out.println(string.toString().length());
str1+="de daima";
}
System.out.println(string);
br.close();
}
}
E:\\test.txt:
1245哈哈
1a 2 b 3 c 4哈哈
a1b23c4d你好吗
黑色能量不想怼你所以不会抛出异常
希望你们也会很友好的出题
syso:
7
22
33
哈哈哈哈哈哈哈 a哈b哈c哈哈哈a哈b哈哈c哈d你好吗黑色能量不 想怼你所以不会抛出异常
2.2对手题目
1.packagedays30;
publicclass Test01 {
staticint arr[];
publicstatic void main(String[] args) {
Stringstr1="abc";
Stringstr2=new String("abc");
inta=(int)Math.abs(Math.round(-1.55));
switch(a){
case1:
System.out.println(arr);
case2:
System.out.println(++a);
case3:
System.out.println(a==Math.round(Math.abs(- 2.55)));
System.out.println(str1==str2);
System.out.println((double)a++);
break;
}
}
}
上述题目有错误吗?若有请指出,若没有,请写出结果.
3
false
flase
3.0
答案
3
true
false
3.0
2.packagework3;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
publicclass g {
publicstatic void main(String[] args) throws IOException {
//TODO Auto-generated method stub
Filef = new File("D:\\data.txt");
DataOutputStreamdou = new DataOutputStream(new FileOutputStream(f));
//写数据
dou.write(1);
dou.writeBoolean(false);
dou.writeByte(1);
dou.writeChar('a');
System.out.println(f.length());
dou.writeFloat(5.2f);
dou.writeUTF("深&圳");
System.out.println(f.length());
dou.writeChars("abc\r\n");
System.out.println(f.length());
dou.flush();
dou.close();
}
}
5
18
28
3.
publicclass Demo03 {
static{
intx = 5;
}
staticint x, y;
publicstatic void main(String args[]) {
x--;
myMethod();
System.out.println(x-y---x);
}
publicstatic void myMethod() {
y= x-- - --x;
}
}
求输出结果:
0
答案:-2
4.publicclass Person {
static{
System.out.print('1');
}
publicPerson(){
System.out.print('2');
}
{
System.out.print(2);
}
voidrun(){
System.out.print('4');
}
}
classStudent extends Person{
static{
System.out.print('a');
}
publicStudent(){
super();
System.out.print('b');
}
{
System.out.print('c');
}
protectedvoid run(){
System.out.print('3');
}
}
publicclass Demo {
publicstatic void main(String[] args) {
Personp1=new Student();
Students=new Student();
s.run();
}
}
请输出运行结果?如果觉得代码运行有问题请说明
不能运行子类重写父类的方法的修饰符要大于或等于父类方法的修饰符 protected
答案:1a22cb22cb3
5.下面有哪些选项是错的()ABCDE
A:构造函数可以调用构造代码块中的变量
B:要创建内部类的对象必须先建立外部类对象
C:当使用Iterator遍历Collection时,Collection不能被改变, 否则会报错。
D:入口函数main(String[]args)不可以被重载
E.int[][]a= {{1,2,3},{3,2},{1}};
int[][]b= a.clone();
a[0][1]=3;
以上代码运行后,b仍位{{1,2,3},{3,2},{1}}
答:
ABDE
学习心得:
1.我热爱工作,我只爱工作