老师,我发现获取Class对象的这三个方法,都是需要你已知Class对象,然后再给我们输出Class对象,这么做好像有点多此一举啊
我没有写flush为什么也可以打印出来呢?
package com.xykj; public class StopThread implements Runnable{ private boolean flag = true; public void stop(){ this.flag = false; } @Override public void run() { System.out.println(Thread.currentThread().getName()+"线程开始"); int i = 0; while(flag){ System.out.println(Thread.currentThread().getName() + " " + i++); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+"线程结束"); } public static void main(String[] args) throws Exception{ System.out.println("主线程开始"); StopThread st = new StopThread(); Thread t1 = new Thread(st); t1.start(); System.in.read(); st.stop(); System.out.println("主线程结束"); } }
为什么在键盘上只有输入空格才会结束流阻塞?
com.xykjStopThread Runnable{ = (){ .= } () { System..println(Thread.().getName()+)i = (){ System..println(Thread.().getName() + + i++){ Thread.()} (InterruptedException e) { e.printStackTrace()} } System..println(Thread.().getName()+)} (String[] args) Exception{ System..println()StopThread st = StopThread()Thread t1 = Thread(st)t1.start()System..read()st.stop()System..println()} }
为什么在键盘只有输入回车才会结束流阻塞。
关于add和append方法,
在StringBuilder中,是没有add方法,添加元素是用append的方法,但是ArrayList中,是使用add的。
这个在实际编程中很容易记混,所以想问下,使用add或者append,是基于什么逻辑,才导致一种容器使用append,另一种容器使用add吗?还是说这个是记下来的,没有什么特殊的逻辑
老师,请问一下这里的dos.writeLong(n);的作用是什么呢
为什么这个类里定义了静态方法泛型在测试时可以用类目去调用这个泛型方法啊
为什么把Users类中CompareTo方法的return -1和return 1交换就能在TreeSet中交换元素顺序?有点不太懂。
老师,是主线程先开始执行,然后两个子线程并行执行,是吗?
还主线程,子线程没有先后顺序,主线程子线程一起并行运行?
老师,这红色方框是运行主线程,而蓝色方框试运行两个子线程,是这样吗?
还有为什么我蓝色方框里的结果没有交错输出?
public void copyFile01(String str1,String str2){ FileReader fr=null; BufferedReader br=null; FileWriter fw=null; BufferedWriter bw=null; try{ br=new BufferedReader(new FileReader(str1)); bw=new BufferedWriter(new FileWriter(str2)); int temp=0; while ((temp= br.read())!=-1){ bw.write(temp); } bw.flush(); }catch (Exception e){ e.printStackTrace(); }finally { try { if (br!=null) br.close(); if (fr!=null) fr.close(); if (bw!=null) bw.close(); if (fw!=null) fw.close(); }catch (Exception e){ e.printStackTrace(); } } } }
没有使用readLine()方法,直接使用的是read()方法,没有加换行,直接就换行了,老师讲讲区别
package com.xykj; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; public class DataOutputDemo { public static void main(String[] args) { DataOutputStream dos = null; try{ dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("d:/data.txt"))); dos.writeChar('a'); dos.write(10); dos.writeDouble(Math.random()); dos.writeBoolean(true); dos.writeUTF("你好"); dos.flush(); }catch (Exception e){ e.printStackTrace(); }finally{ try{ if (dos != null){ dos.close(); } }catch (Exception e){ e.printStackTrace(); } } } } package com.xykj; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.FileInputStream; public class DataInputDemo { public static void main(String[] args) { DataInputStream dis = null; try{ dis = new DataInputStream(new BufferedInputStream(new FileInputStream("d:/data.txt"))); //直接读取数据,读取的顺序要与写入的顺序一致,否则不能正确读取数据。 System.out.println("char: "+dis.readChar()); System.out.println("int: "+dis.readInt()); System.out.println("double: "+dis.readDouble()); System.out.println("boolean: "+dis.readBoolean()); System.out.println("String: "+dis.readUTF()); }catch(Exception e){ e.printStackTrace(); }finally { try{ if (dis != null){ dis.close(); } }catch (Exception e){ e.printStackTrace(); } } } }
老师,我是按照顺序读取的,怎么还会报错
com.xykjjava.io.BufferedOutputStreamjava.io.DataOutputStreamjava.io.FileOutputStreamDataOutputDemo { (String[] args) { DataOutputStream dos = { dos = DataOutputStream(BufferedOutputStream(FileOutputStream()))dos.writeChar()dos.write()dos.writeDouble(Math.())dos.writeBoolean()dos.writeUTF()dos.flush()}(Exception e){ e.printStackTrace()}{ { (dos != ){ dos.close()} }(Exception e){ e.printStackTrace()} } } }
com.xykjjava.io.BufferedInputStreamjava.io.DataInputStreamjava.io.FileInputStreamDataInputDemo { (String[] args) { DataInputStream dis = { dis = DataInputStream(BufferedInputStream(FileInputStream()))System..println(+dis.readChar())System..println(+dis.readInt())System..println(+dis.readDouble())System..println(+dis.readBoolean())System..println(+dis.readUTF())}(Exception e){ e.printStackTrace()}{ { (dis != ){ dis.close()} }(Exception e){ e.printStackTrace()} } } }
老师,我是按着顺序读取的,怎么还会报错!
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class ServerSocketTest { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; BufferedReader br = null; Scanner scanner = null; PrintWriter pw = null; try { serverSocket = new ServerSocket(8888); System.out.println("服务端启动!监听端口8888..."); socket = serverSocket.accept(); //创建从客户端读取信息的流对象 br = new BufferedReader(new InputStreamReader(socket.getInputStream())); //创建键盘输入对象 scanner = new Scanner(System.in); //创建向客户端发送消息的输出流对象 pw = new PrintWriter(socket.getOutputStream()); while (true){ //读取客户端发送的信息 String str = br.readLine(); System.out.println("客户端说:" + str); String keyInput = scanner.nextLine(); //发送到客户端 pw.println(keyInput); pw.flush(); } }catch (Exception e){ e.printStackTrace(); }finally { if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (scanner != null){ scanner.close(); } if (pw != null){ pw.close(); } } } }
老师,我的代码没有什么问题啊,可是在运行的时候,提示在定义serverSocket时出问题,老师能帮我看看吗
服务端和客户端都有同样的错误
public int menuItemAvalizble(int min,int max){ //定义正则表达式 String regex="[1-9]"; Scanner sc=new Scanner(System.in); while (true){ System.out.println("请输入您要操作的内容"+min+"-"+max); String input=sc.nextLine(); int value=Integer.parseInt(input); if (input.matches(regex)){ if (value>=min&&value<=max) return value; else System.out.println("您输入的内容错误,请重新输入"); }else System.out.println("您输入的内容不合法,请输入1~6之间的数字"); } }
老师,这个方法规定了第一次输入的字符串类型只能是数字字符串,如果输入字母就会报异常;有啥好的解决方法嘛
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637