这节课是为了演示转换流才用字节流吧,一般什么情况需要用字节流读文本文件?
复制出来的图片文件是空的可能是什么问题呢?前面几个实验复制都成功了
下面文件路径我有小小遮下
package com.bjsxt; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; public class FileStreamBuffer3Demo { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { fis = new FileInputStream("/Users/.../Documents/...jpg"); bis = new BufferedInputStream(fis); fos = new FileOutputStream("/Users/.../Documents/...jpg"); bos = new BufferedOutputStream(fos); int temp = 0; while((temp = bis.read()) != -1){ bos.write(temp); } bos.flush(); }catch (Exception e ){ e.printStackTrace(); }finally { try { }catch (Exception e){ e.printStackTrace(); } } } }
public class TestThread2 implements Runnable{ public TestThread2(){ System.out.println(Thread.currentThread().getName()); } @Override public void run() { System.out.println(Thread.currentThread().getName()+"线程开始"); for (int i = 0;i<20;i++){ System.out.println(Thread.currentThread().getName()+" "+i); } System.out.println(Thread.currentThread().getName()+"线程结束"); } public static void main(String[] args) { System.out.println("主线程开始"); TestThread2 testThread2 = new TestThread2(); Thread t1 = new Thread(testThread2); t1.start(); Thread t2 = new Thread(new TestThread2()); t2.start(); System.out.println("主线程结束"); } }
为什么我这个线程是按顺序来的?
2022版本的平行运行在哪儿呀
public void start() { Menu menu = new Menu(); TelNoteRegex regex = new TelNoteRegex(); Operate operate = new Operate(); File file = new File("/Users/JAVA/a.txt"); ObjectInputStream ois = null; Object o = null; try { file.createNewFile(); ois = new ObjectInputStream(new FileInputStream(file)); if ((o = ois.readObject())==null){ return; }else { operate.ObjectInputStreamDemo(); } } catch (Exception e) { e.printStackTrace(); } while (true) { menu.mainMenu(); int item = regex.menuItemValidate(1, 6); switch (item) { case 1: operate.addLogic(); break; case 2: operate.searchLogic(); break; case 3: operate.modifyLogic(); break; case 4: operate.deleteLogic(); break; case 5: operate.orderLogic(); break; case 6: operate.ObjectOutputStreamDemo(); System.exit(0); } } }
我在这里做了判断了,为什么还会报EOF?
public void ObjectOutputStreamDemo(){ ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("/Users/JAVA/a.txt",true)); for (Person temp:list){ oos.writeObject(temp); } oos.flush(); }catch (Exception e){ e.printStackTrace(); }finally { try { if (oos!=null){ oos.close(); } }catch (Exception e){ e.printStackTrace(); } } } public void ObjectInputStreamDemo(){ ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("/Users/JAVA/a.txt")); Person a = null; while(true){ if ((a = (Person) ois.readObject())==null){ break; } Person person =(Person) ois.readObject(); this.list.add(person); } }catch (Exception e){ e.printStackTrace(); }finally { try { if(ois!=null){ ois.close(); } }catch (Exception e){ e.printStackTrace(); } } }
StreamCorruptedException这个报错我该怎么修改呢,有时候if语句中还会报类型强制转换的错误是为什么呢
老师,需要你的帮助文档 java8 api。 可否分享
我先对没有加get方法的users进行序列化,再反序列化的时候再去users类中添加get方法,进行反序列化的时候报错了,这是因为什么原因呢
为什么输入到输出会自己换行,输出到输入要自己添加换行呢
老师,守护线程是在哪个线程里面被设置,就随那个线程结束嘛?
老师,这里用的PrintWriter属于节点流还是处理流?好像既可以包装字节流,也可以直接填写文件地址呢?还有如果他是字符流的话,里面是字节流字符流都可以放嘛?
try catch 跟throws的区别能详细讲一下吗
if(c=='{'){ stack.push("}");
为什么c == ’{‘ 还要添加对应的 ’} ‘不是很理解
老师您好,我想请问 是否可以把Map中容器的元素理解为自带名字(键)的值?这个键的描述听起来很像下标啊
package com.dataStructure; /** * 自定义基于单向链表实现存储数据的容器 */ public class MySinglyLinkedList<E> implements MyList<E>{ private myNode head; private int size; /** * 创建一个单线链表中用于存储数据的节点类 * @param <E> */ class myNode<E>{ private E item; private myNode next ; public myNode(E item, myNode next) { this.item = item; this.next = next; } } private myNode<E> getTail (){ myNode h = this.head; if(this.head == null){ return null; } while (true){ if(head.next == null){ break; }else { h = h.next; } }return h; } /** * 根据下标获取节点的方法 */ private myNode getNode(int index){ myNode n = head; for (int i = 0 ; i < index; i++){ n = n.next; } return n; } /** * 校验下标合法性的方法 */ private void CheckIndex (int index){ if(this.head == null||index>=size||index<0){ throw new IndexOutOfBoundsException(); } } @Override /** * 添加元素的方法 */ public void add(Object element) { myNode node = new myNode<>(element,null); myNode tail = getTail(); if(tail == null){ this.head = node; }else { tail.next = node; } this.size++; } @Override /** * 删除元素的方法 */ public E remove(int index) { CheckIndex(index); myNode node = getNode(index); E item = (E)node.item; //找到后把这个元素的前一个的next挂到此元素后一个 if(node == this.head){//判断此节点是否是头结点 this.head = node.next; }else { for (int f = 0 ; f < index-1; f++){ getNode(f).next = node.next; } } this.size--; return item; } @Override /** * 获取元素的方法 */ public E get(int index) { this.CheckIndex(index); return (E) getNode(index).item; } @Override /** * 返回元素的个数 */ public int size() { return this.size; } } //新建测试类 package com.dataStructure; public class TestMySinglyLinkedList { public static void main(String[] args) { MySinglyLinkedList<String> mySinglyLinkedList = new MySinglyLinkedList(); mySinglyLinkedList.add("a"); mySinglyLinkedList.add("b"); mySinglyLinkedList.add("c"); mySinglyLinkedList.add("d"); mySinglyLinkedList.add("e"); mySinglyLinkedList.add("f"); System.out.println(mySinglyLinkedList.size()); System.out.println(mySinglyLinkedList.get(3)); System.out.println(mySinglyLinkedList.remove(5)); System.out.println(mySinglyLinkedList.size()); } }
我有两个疑问:1、我执行测试类的时候,报了空指针异常,不懂为什么;
2、为什么我初始化E类型的变量时,要让我强转为E类型?
老师,你好:
现在已经理解了守护线程的特点和生命周期。但是它到底守护了个啥???没有get到核心的点。
谢谢
燕
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637