当前位置: 首页 > 编程日记 > 正文

你真的以为了解java.io吗 呕心沥血 绝对干货 别把我移出首页了

文章结构
1 flush的使用场景
2 一个java字节流,inputstream 和 outputstream的简单例子
3 分别测试了可能抛出java.io.FileNotFoundException,java.io.FileNotFoundException: test (拒绝访问。),java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)的所有场景,你再也不怕java.io异常了
4 测试了flush的使用场景
5 给出了flush的源码
6 提出了自己的两个小疑问
1 flush的有效使用场景
outputstream中有flush()方法, 而inputstream没有用到缓冲区,对于字节流来说,缓冲区就是一个byte数组,而OutputStream类的flush()却什么也没做,其实flush()是Flushable接口的方法,官方文档的对该方法的注释是“Flushes this output stream and forces any buffered output bytes to be written out.”。OutputStream方法实现了Flushable接口,而又什么也没做,真是让人一头雾水,那么什么时候flush()才有效呢?当OutputStream是BufferedOutputStream时。当写文件需要flush()的效果时,需要需要将FileOutputStream作为BufferedOutputStream构造函数的参数传入,然后对BufferedOutputStream进行写入操作,才能利用缓冲及flush()。查看BufferedOutputStream的源代码,发现所谓的buffer其实就是一个byte[]。BufferedOutputStream的每一次write其实是将内容写入byte[],当buffer容量到达上限时,会触发真正的磁盘写入。而另一种触发磁盘写入的办法就是调用flush()了。
2一个java字节流,inputstream 和 outputstream的简单例子
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;public class OutputStreamDemo {public static void main(String[] args) {try {// create a new output streamOutputStream os = new FileOutputStream("test.txt");// craete a new input streamInputStream is = new FileInputStream("test.txt");// write somethingos.write('A');// flush the stream but it does nothing
             os.flush();// write something elseos.write('B');// read what we wroteSystem.out.println("" + is.available());} catch (Exception ex) {ex.printStackTrace();}}}
说明我们的os.flush()什么也没有做
其中  OutputStream os = new FileOutputStream("test.txt");将在我们项目下的根文件下建立一个TXT文件,FileOutputStream创建一个向具有指定名称的文件中写入数据的输出文件流。创建一个新 FileDescriptor 对象来表示此文件连接。如果有安全管理器,则用 name 作为参数调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException,
3 测试java.io异常
当我们的String name定义为一个空字符串的时候 抛出如下异常
java.io.FileNotFoundException:
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at OutputStreamDemo.OutputStreamDemo.main(OutputStreamDemo.java:14)
当我们在根目录下定义一个文件夹也名为test的文件夹时,会抛出如下异常
java.io.FileNotFoundException: test (拒绝访问。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at OutputStreamDemo.OutputStreamDemo.main(OutputStreamDemo.java:14)
当我们的FileOutputStream("test.txt");和FileInputStream("test.txt");中的字符串不一致时会出现异常
java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at OutputStreamDemo.OutputStreamDemo.main(OutputStreamDemo.java:17)
我们这里用了 os.write('A');这里的'A'是int类型的
4 测试了flush的使用场景
OutputStream os = new FileOutputStream("test.txt");// craete a new input streamInputStream is = new FileInputStream("test.txt");// write somethingBufferedOutputStream bos = new BufferedOutputStream(os);bos.write('C');// flush the stream but it does nothing
             bos.flush();// write something elsebos.write('B');bos.close();

如果我们不使用flush,也不使用close的话,则输出结果为0
5 给出了flush的源码

通过查看源码,可以看到 BufferedOutputStream中的flush实现如下 BufferedOutputStream是同步的

public synchronized void flush() throws IOException {flushBuffer();out.flush();}
private void flushBuffer() throws IOException {if (count > 0) {out.write(buf, 0, count);count = 0;}}public void write(byte b[], int off, int len) throws IOException {if (b == null) {throw new NullPointerException();} else if ((off < 0) || (off > b.length) || (len < 0) ||((off + len) > b.length) || ((off + len) < 0)) {throw new IndexOutOfBoundsException();} else if (len == 0) {return;}for (int i = 0 ; i < len ; i++) {write(b[off + i]);}}

6 提出了自己的两个小疑问

我的小疑问:

1 小疑问 另外,在我使用editplus打开该文件的时候,我任然可以执行java命令重新对该文件进行写入,这让我想到了java的多线程的锁及数据库的锁
也对,在我用editplus打开该文件的时候仅仅是执行读取得权限,而其他应用程序是可以进行更改的
2 当我使用java程序更改该文件的时候,我在打开editplus窗口的时候,提示我the file has been modified by another programmer ,do you want to reload it.这说明editplus有一个监视器随时在监视该文件的状态

转载于:https://www.cnblogs.com/winAlaugh/p/5459654.html

相关文章:

GitHub为所有人免费提供了所有核心功能-这就是您应该关心的原因

Just a couple of days ago, GitHub wrote a blog article stating that it is now free for teams. Heres the official blog article if youre interested. 就在几天前&#xff0c;GitHub写了一篇博客文章&#xff0c;指出它现在对团队免费。 如果您有兴趣&#xff0c;这是官…

什么是ObjCTypes?

先看一下消息转发流程: 在forwardInvocation这一步&#xff0c;你必须要实现一个方法: - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE(""); 该方法用于说明消息的返回值和参数类型。NSMethodSignature是方法签名&#x…

0基础JavaScript入门教程(一)认识代码

1. 环境&#xff1a; JavaScript简称js&#xff0c;后续我们将使用js来代替JavaScript。 认识代码前&#xff0c;需要安装js代码运行环境。 安装nodejs&#xff1a;在https://nodejs.org/zh-cn/ 下载LTS版本&#xff0c;然后安装安装visual studio code&#xff1a;https://…

junit、hamcrest、eclemma的安装与使用

1、junit的安装与使用 1.1 安装步骤 1&#xff09;从http://www.junit.org/ 下载junit相应的jar包&#xff1b; 2&#xff09; 在CLASSPATH中加入JAR包所在的路径&#xff0c;如E:\Java\jar\junit\junit-4.10.jar&#xff1b; 3&#xff09; 将junit-4.10.jar加入到项目的lib文…

如何撰写将赢得客户青睐的自由职业者提案和免费模板

Your prospective client asks you to provide them with a quote. So you just send them the quote, right?您的潜在客户要求您提供报价。 所以您只给他们发送报价吧&#xff1f; Wrong.错误。 If you did, you would be missing out on a massive opportunity here.如果这…

2. 把一幅图像进行平移。

实验二 #include "cv.h" #include<stdio.h> #include "highgui.h" IplImage *PingYi(IplImage *src, int h0, int w0); int main(int argc, char** argv) {IplImage* pImg; //声明IplImage指针IplImage* pImgAfterMove;pImg cvLoadImage("601…

后台的代理nginx部署方法

软件包如下&#xff1a;nginx-1.10.0.tar.gznginx-http-concat-master.zipngx_cache_purge-2.3.tar.gzopenssl-1.0.2h.tar.gzpcre-8.39.tar.gzzlib-1.2.8.tar.gz ngin部署方法&#xff1a;上面的安装包都存放在/apps/svr/soft目录下:cd /apps/svr/softtar -zxf nginx-1.10.0.ta…

iOS中你可能没有完全弄清楚的(一)synthesize

1. 什么是synthesize synthesize中文意思是合成&#xff0c;代码中我们经常这样用。 interface Test: NSObject property (nonatomic, unsafe_unretained) int i; endimplementation Test synthesize i; end 复制代码 使用synthesize的2个步骤&#xff1a; 首先你要有在类声…

framer x使用教程_如何使用Framer Motion将交互式动画和页面过渡添加到Next.js Web应用程序

framer x使用教程The web is vast and its full of static websites and apps. But just because those apps are static, it doesnt mean they have to be boring. 网络非常庞大&#xff0c;到处都是静态的网站和应用。 但是&#xff0c;仅仅因为这些应用程序是静态的&#xf…

POJ 2429

思路&#xff1a;a/n*b/nlcm/gcd 所以这道题就是分解ans.dfs枚举每种素数情况。套Miller_Rabin和pollard_rho模板 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c交2 #include<cstdio>3 #include<cstring>4 #include<cstdlib…

iOS中你可能没有完全弄清楚的(二)自己实现一个KVO源码及解析

前几天写了一篇blog&#xff08;点这里&#xff09;&#xff0c;分析了系统KVO可能的实现方式。并添加了简单代码验证。 既然系统KVO不好用&#xff0c;我们完全可以根据之前的思路&#xff0c;再造一个可以在项目中使用的KVO的轮子。 代码已经上传到github: https://github.…

js中的preventDefault与stopPropagation详解

1. preventDefault: 比如<a href"http://www.baidu.com">百度</a>,这是html中最基础的东西&#xff0c;起的作用就是点击百度链接到http://www.baidu.com,这是属于<a>标签的默认行为;preventDefault方法就是可以阻止它的默认行为的发生而发生其他…

angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器

angular过滤字符介绍 (Introduction) In this article, we will create an optical character recognition (OCR) application using Angular and the Azure Computer Vision Cognitive Service. 在本文中&#xff0c;我们将使用Angular和Azure计算机视觉认知服务创建一个光学字…

javascript函数全解

0.0 概述 本文总结了js中函数相关的大部分用法&#xff0c;对函数用法不是特别清晰的同学可以了解一下。 1.0 简介 同其他语言不同的是&#xff0c;js中的函数有2种含义。 普通函数&#xff1a;同其他语言的函数一样&#xff0c;是用于封装语句块&#xff0c;执行多行语句的…

MYSQL explain详解[转载]

explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。 虽然这篇文章我写的很长&#xff0c;但看起来真的不会困啊&#xff0c;真的都是干货啊&#xff01;&#xff01;&#xff01;&#xff01; 先解析一条sql语句&…

CodeForces 157A Game Outcome

A. Game Outcometime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSherlock Holmes and Dr. Watson played some game on a checkered board n  n in size. During the game they put numbers on the boards squares…

我使用Python和Django在自己的网站上建立了一个会员专区。 这是我学到的东西。

I decided it was time to upgrade my personal website in order to allow visitors to buy and access my courses through a new portal. 我认为是时候升级我的个人网站了&#xff0c;以允许访问者通过新的门户购买和访问我的课程 。 Specifically, I wanted a place for v…

详解AFNetworking的HTTPS模块

0.0 简述 文章内容包括&#xff1a; AFNetworking简介ATS和HTTPS介绍AF中的证书验证介绍如何创建服务端和客户端自签名证书如何创建简单的https服务器对CA正式证书和自签名证书的各种情况进行代码验证 文中所涉及的文件和脚本代码请看这里。 1.0 AFNetworking简介 AFNetwo…

字符串专题:map POJ 1002

第一次用到是在‘校内赛总结’扫地那道题里面&#xff0c;大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser] 自动存donser到map并且值加一&#xff0c;如果发现重复元素不新建直接加一&#xff0c; map第一个参数是key&…

【洛谷P1508】吃吃吃

题目背景 问世间&#xff0c;青春期为何物&#xff1f; 答曰&#xff1a;“甲亢&#xff0c;甲亢&#xff0c;再甲亢&#xff1b;挨饿&#xff0c;挨饿&#xff0c;再挨饿&#xff01;” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达&#xff0c;最近一直处…

前端和后端开发人员比例_前端开发人员vs后端开发人员–实践中的定义和含义

前端和后端开发人员比例Websites and applications are complex! Buttons and images are just the tip of the iceberg. With this kind of complexity, you need people to manage it, but which parts are the front end developers and back end developers responsible fo…

Linux 创建子进程执行任务

Linux 操作系统紧紧依赖进程创建来满足用户的需求。例如&#xff0c;只要用户输入一条命令&#xff0c;shell 进程就创建一个新进程&#xff0c;新进程运行 shell 的另一个拷贝并执行用户输入的命令。Linux 系统中通过 fork/vfork 系统调用来创建新进程。本文将介绍如何使用 fo…

metasploit-smb扫描获取系统信息

1.msfconsle 2.use auxiliary/scanner/smb/smb_version 3. msf auxiliary(smb_version) > set RHOSTS 172.16.62.1-200RHOSTS > 172.16.62.1-200msf auxiliary(smb_version) > set THREADS 100THREADS > 100msf auxiliary(smb_version) > run 4.扫描结果&#x…

算法(1)斐波那契数列

1.0 问题描述 实现斐波那契数列&#xff0c;求第N项的值 2.0 问题分析 斐波那契数列最简单的方法是使用递归&#xff0c;递归和查表法同时使用&#xff0c;可以降低复杂度。根据数列特点&#xff0c;同时进行计算的数值其实只有3个&#xff0c;所以可以使用3个变量循环递进计…

主键SQL教程–如何在数据库中定义主键

Every great story starts with an identity crisis. Luke, the great Jedi Master, begins unsure - "Who am I?" - and how could I be anyone important? It takes Yoda, the one with the Force, to teach him how to harness his powers.每个伟大的故事都始于…

算法(2)KMP算法

1.0 问题描述 实现KMP算法查找字符串。 2.0 问题分析 “KMP算法”是对字符串查找“简单算法”的优化。字符串查找“简单算法”是源字符串每个字符分别使用匹配串进行匹配&#xff0c;一旦失配&#xff0c;模式串下标归0&#xff0c;源字符串下标加1。可以很容易计算字符串查…

告别无止境的增删改查:Java代码生成器

对于一个比较大的业务系统&#xff0c;我们总是无止境的增加&#xff0c;删除&#xff0c;修改&#xff0c;粘贴&#xff0c;复制&#xff0c;想想总让人产生一种抗拒的心里。那有什么办法可以在正常的开发进度下自动生成一些类&#xff0c;配置文件&#xff0c;或者接口呢&…

Maven国内源设置 - OSChina国内源失效了,别更新了

Maven国内源设置 - OSChina国内源失效了&#xff0c;别更新了 原文&#xff1a;http://blog.csdn.net/chwshuang/article/details/52198932 最近在写一个Spring4.x SpringMVCMybatis零配置的文章&#xff0c;使用的源配的是公司的私有仓库&#xff0c;但是为了让其他人能够通过…

如何使用Next.js创建动态的Rick and Morty Wiki Web App

Building web apps with dynamic APIs and server side rendering are a way to give people a great experience both with content and speed. How can we use Next.js to easily build those apps?使用动态API和服务器端渲染来构建Web应用程序是一种使人们在内容和速度上都…

安装部署Spark 1.x Standalone模式集群

Configuration spark-env.sh HADOOP_CONF_DIR/opt/data02/hadoop-2.6.0-cdh5.4.0/etc/hadoop JAVA_HOME/opt/modules/jdk1.7.0_67 SCALA_HOME/opt/modules/scala-2.10.4 ####################################################### #主节点 …