Java writer reader与相关练习
来源:互联网 发布:双击屏幕唤醒软件 编辑:程序博客网 时间:2024/06/06 06:36
学习心得
一、专业课
1.Reader与Writer
FileReader与FileWriter
BufferedReader与BufferedWriter
缓冲流可以提高效率,安全
比较缓冲流与节点交流的读取文件的区别
publicstatic void main(String[] args) {
Filesrc = new File("d:/d.pdf");
Filedest = new File("e:/d.pdf");
longstart1 = System.currentTimeMillis();
try{
compareBytes(src,dest);
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
longend1 = System.currentTimeMillis();
System.out.println(end1-start1);
longstart2 = System.currentTimeMillis();
try{
compareBytes2(src,dest);
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
longend2 = System.currentTimeMillis();
System.out.println(end2-start2);
}
publicstatic void compareBytes(File src,File dest) throws IOException {
FileInputStreamfis = null;
FileOutputStreamfos = null;
try{
fis= new FileInputStream(src);
fos= new FileOutputStream(dest);
byte[]bs = new byte[1024];
intlen = 0;
while(-1 != (len = fis.read(bs))) {
fos.write(bs,0, len);
}
fos.flush();
}catch(Exception e) {
e.printStackTrace();
}finally{
if(null != fos ) {
fos.close();
}
if(null != fis) {
fis.close();
}
}
}
publicstatic void compareBytes2(File src,File dest) throws IOException {
FileInputStreamfis = null;
FileOutputStreamfos = null;
BufferedInputStreambis = null;
BufferedOutputStreambos = null;
try{
fis= new FileInputStream(src);
fos= new FileOutputStream(dest);
//字节缓冲流默认大小为8KB
bis= new BufferedInputStream(fis);
bos= new BufferedOutputStream(fos);
byte[]bs = new byte[1024];
intlen = 0;
while(-1 != (len = bis.read(bs))) {
bos.write(bs,0, len);
}
bos.flush();
}catch(Exception e) {
e.printStackTrace();
}finally{
if(null != bos ) {
bos.close();
}
if(null != bis) {
bis.close();
}
}
}
}
2.小组PK
2.1我方题目
1.阅读以下代码,若能运行则输出运行结果(异常信息输出可以忽略),若不能运行请说明原因:
publicclass Note {
publicstatic void main(String[] args) {
System.out.println(fun(0));
}
publicstatic int fun(int a) {
intsum = 0;
try{
Stack<String>stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("c");
stack.push("d");
for(int i = 0; i < stack.size(); i++) {
System.out.println(stack.pop());
}
Iterator<String>iterator = stack.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
sum= 3 / a;
}catch (ArithmeticException e) {
e.printStackTrace(); //(1)
returna; //(2)
}finally {
a++;
System.out.println("sum:"+ sum + a);
}
returnsum;
}
}
d
c
a
b
sum:01 //其他输出都对
1 //步骤(1)执行后会将a的值压栈,所以此时a的值为0,最后返回的也是这个值,输出0
2.写出下列程序输出结果。
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
chara = 1;
intb = 1;
intf = 2;
Longg = 3l;
Integere = 3;
Integerc = 321;
Integerd = 321;
Stringstr = "abc";
Stringstr3 = "abc";
StringBufferstr1 = new StringBuffer("abc");
StringBuilderstr2 = new StringBuilder("abc");
System.out.println(a== b);
System.out.println(g== (a + f));
System.out.println(c== d);
System.out.println(e== (b + f));
System.out.println(str.equals(str1));
System.out.println(str1.equals(str2));
System.out.println(str2.equals(str));
System.out.println(str== str3);
}
true
true
false
true
false
false
false
true
3.
publicclass Test {
publicstatic void main(String[] args) throws IOException {
FileOutputStreamfou = new FileOutputStream(newFile("D:"+File.separator+"hx.txt"));
FileOutputStreamfouu = new FileOutputStream(newFile("D:"+File.separator+"hx.txt"));
FileOutputStreamfouuu = new FileOutputStream(newFile("D:"+File.separator+"hx.txt"),true);
Stringstr = "hahaha黄品民是个大神!";
Stringstr1 = "hahaha阮凯也是个大神!";
Stringstr2 = "hahaha何清也是个大神!";
Stringstr3 = "hahaha康路军也是个大神!";
Stringstr4 = "hahaha何星仰望哈哈哈!";
fou.write(str.getBytes());
fou.flush();
fou.write(str1.getBytes());
fou.flush();
//请写出此时hx.txt文本中的内容
fouu.write(str2.getBytes());
//请写出此时hx.txt文本中的内容
fouuu.write(str3.getBytes());
fouuu.write(str4.getBytes());
fouuu.flush();
//请写出此时hx.txt文本中的内容
fou.close();
fouu.close();
fouuu.close();
}
}
答案:
1:
hahaha黄品民是个大神!hahaha阮凯也是个大神!
2:
hahaha何清也是个大神!hahaha阮凯也是个大神!
3:
hahaha何清也是个大神!hahaha阮凯也是个大神!hahaha康路军也是个大神!hahaha何星仰望哈哈哈!
4.
请找出下面程序语句的错误,并在错误语句后打*(错误可能有几处)
publicclass Test04 {
publicstatic void main(String[] args) throws IOException {
Integeri = -2;
Doubled = -4; //* //包装类后面需要d
List<String>list = new ArrayList<>();
list.add(null);
Filefile = new File("D:\pd"); //* \这个杆有错
FileOutputStreamfos = new FileOutputStream(file);
Stringstr = "*";
intlen = 0 ;
byte[]bytes = new byte[1024];
while((len= fos.write(str.getBytes())) != -1){ //*fos.write方法没有返回值
System.out.println(len);
}
System.out.println(len);
fos.close();
}
}
5.请仔细 阅读 下段代码,若有错误,请改正并写出syso的结果
若没有发现错误,请写出你认为会输出的结果
//省略各种工具包,类
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() ).length < 20){ //String.length() !!
System.out.println(string.length());
string= string.replaceAll("\\d", "/3");
System.out.println(string);
}
br.close();
}
test内容:
112
334
345
456
4567
567
结果:
3
/3/3/3
9
//3//3//3/3/3/3
18
///3///3///3//3//3//3/3/3/3
2.2对手题目
1.FileInputStream、FileReader、BufferedInputStream、BufferedRe ader中哪些的read()方法能够将内容读至字符数组中read(byte[] b)?
FileInputStream
BufferedInputStream
正确
2.packagecom.javaio;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.OutputStream;
publicclass Demo01 {
publicstatic void main(String[] args) throws Exception {
File f = new File("E:" + File.separator + "test.txt");
OutputStream out = new FileOutputStream(f);
String str = "!中国"+"!Hello_World_12345_67890";//第一个感叹号为中文符 第二个为英文符 字符串中无空格
byte b[] = str.getBytes();
for(int i = 0; i < b.length && i % 8 != 7 ;i++){
out.write(b[i]);
}
out.close();
System.out.println("输出成功");
}
}
写出test.txt的内容
!中国!
3.
importjava.util.Stack;
publicclass Test02 {
publicstatic void main(String[] args) {
Stack<String>a=new Stack<>();
a.push("1");
a.push("a");
a.push("2");
a.push("b");
a.push("3");
a.push("c");
for(int i = 0; i < a.size(); i++) {
System.out.println(a.pop());
i++;
}
}
}
运行结果是?
c
3
4.interface A
{
intx = 0;
}
classB
{
intx =1;
}
classC extends B implements A
{
publicvoid pX()
{
System.out.println(x);
}
publicstatic void main(String[] args) {
newC().pX();
}
}
程序能否通过编译?如果不能请说明?如果能写出输出结果
不能。在编译时会发生错误(错误描述不同的JVM有不同的信息,意思就是未明确的x调用,两个x都匹配(就象在同时importjava.util和 java.sql两个包时直接声明Date一样)。对于父类的变量,可以用 super.x来明确(输出的是1),而接口的属性默认隐含为publicstatic final.所以可以通过A.x来明确(输出的是0)。
5.
下面选项中正确的是(ABC )
A、java程序:
packagecom.io;
importjava.util.Date;
publicclass Test extends Date {
publicstatic void main(String[] args) {
newTest().test();
}
publicvoid test(){
System.out.println(super.getClass().getName());
}
}
此程序的输出结果为:com.io.Test
B、读写原始数据,一般采用的流是InputStream和OutputStream
C、为了提高读写性能,可以采用BufferedInputStream和 BufferedOutputStream
D、一个File对象代表了操作系统中的一个文件或文件夹
学习心得:
1.山外有山,人外有人
- Java writer reader与相关练习
- java IO Reader Writer
- Java基础----Reader、Writer
- Java IO - Reader&Writer
- 说说 Java I/O 系统之 Reader 与 Writer
- VTK的Reader与Writer
- java io (四) Reader Writer
- Java IO reader and writer
- Java IO之Reader/Writer
- Reader && Writer
- Reader/Writer
- reader,writer与stream的区别
- Reader/Writer与InputStream/OutputStream的区别
- Reader与Writer的IO操作
- Java8 I/O源码-Reader与Writer
- Java Reader and Writer的类结构
- java Reader和Writer来进行输入输出
- Java IO 字符流 Reader和Writer
- win10按两次开机键才能启动系统
- 理解 Android Build 系统
- python 素数
- “玲珑杯”ACM比赛 Round #13 B -- 我也不是B,倍增+二分!
- 在oschina踩的坑
- Java writer reader与相关练习
- imessage使用一
- Oracle(5)使用profile文件对口令进行管理
- Java实现字符与Unicode互转
- H
- Object类中clone()方法的修饰符为什么是protected
- 《易读》一个模仿网易云音乐UI,Rxjava+Retrofit+dagger2+MVP的开源项目
- 解决卡顿——在WPF中使用多线程更新UI
- AIDL实现进程间通信