当前位置: 首页 > news >正文

天津市建设工程备案网站网络营销策划书8000字

天津市建设工程备案网站,网络营销策划书8000字,个人制作网站多少钱,小程序电商商城Java IO流 文章目录Java IO流什么是IO流InputStreamFlieInputStream示例OutputStream示例字符的读取与写入READER方法WRITER方法利用Scanner和PrintWriter简化字符的读写ScannerPrintWriter什么是IO流 前面我们介绍了Java中对文件的操作以及file类的了解,但是file类…

Java IO流

文章目录

  • Java IO流
    • 什么是IO流
    • InputStream
    • FlieInputStream
        • 示例
    • OutputStream
      • 示例
    • 字符的读取与写入
      • READER方法
      • WRITER方法
    • 利用Scanner和PrintWriter简化字符的读写
      • Scanner
      • PrintWriter

什么是IO流

前面我们介绍了Java中对文件的操作以及file类的了解,但是file类只能对文件信息进行操作,创建,获取,删除,列表,重命名。无法进行文件的读取和写入。

在Java中I/O操作是指对文件进行输入Input/输出Output操作。 Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列。Java的I/O流提供了读写数据的标准方法。任何Java中表示数据源的对象都会提供以数据流的方式读写它的数据的方法。

Java中针对InputOutput提供了2个抽象类

InputStream :输入流,把文件数据读取到内存

OutputStream:输出流,把数据从内存输出到文件中

在系统中有很多种的文件类型一般分为 文本文件二进制文件

Java分别对这两种文件类型提供了相应的方法对文件进行读写。

image-20230307001104511

InputStream

常见的方法

intread()读取一个字节的数据,读完返回-1
intread(byte[] b)最多读取b.length个字节的数据
intread(byte[] b,int off,int len)最多读取len-off个字节数据
voidclose()关闭字节流

InputStream只是一个抽象类,要使用还需要具体的实现类。关于InputStream的实现类很多,这里简单的介绍一下FileInputStream

FlieInputStream

FileInputStream(File file)利用File文件构造文件输入流
FileInputStream(File file)利用文件路径创建输入流

示例

利用文件路径读文件内容

public static void main(String[] args) throws IOException {// 创建一个输入流,读取文件内容FileInputStream inputStream=new FileInputStream("d:/JAVA/temp/test/aaa.txt");//用一个byte数组,来接收每次读取的内容,扩大缓冲区容量byte[] bytes=new byte[1024];//读取文件中的数据while (true){//获取每次读取的长度int len=inputStream.read(bytes);if (len==-1){break;}//打印读取到的内容for (int i = 0; i < len; i++) {System.out.println((char) bytes[i]);}//关闭流inputStream.close();}
}

OutputStream

voidwrite(int b)写入要给字节的数据
voidwrite(byte[]b)将b 这个字符数组的全部数据写到os中
intwrite(byte[]b,int off,int len)将b数组中从off开始把数据写到os中,一个写len个
voidclose()关闭流
voidflush()刷新缓冲区,将数据从缓冲区写入文件

在计算机系统中,硬件之间的传输速度是很慢的,大量的OutputStream操作会使操作时间大大加长,因此,在写入数据时,会将写入数据暂时存放在内存的一个指定的区域里,一般称为缓冲区,直到缓冲区满了或者其他条件时,才将缓冲区的数据写入到目标文件中。但有可能会造成一部分数据遗留到缓冲区里,因此调用flush方法,将数据刷到文件中。

示例

public static void main(String[] args) throws IOException {FileOutputStream outputStream=new FileOutputStream("d:/JAVA/temp/test/aaa.txt");outputStream.write(97);outputStream.write(98);outputStream.write(99);//刷新缓冲区outputStream.flush();outputStream.close();
}

字符的读取与写入

READER方法

public static void main(String[] args) throws IOException {FileReader reader=new FileReader("d:/JAVA/temp/test/aaa.txt");while (true){int data=reader.read();//返回-1表示读取结束if (data==-1){break;}System.out.println(data);}reader.close();
}

WRITER方法

public static void main(String[] args) throws IOException {FileWriter writer=new FileWriter("d:/JAVA/temp/test/aaa.txt");writer.write("123\n");writer.write("哈喽");//刷新缓冲区writer.flush();//关闭流writer.close();
}

利用Scanner和PrintWriter简化字符的读写

上述例子中,InputStream、OutputStream对字符类型文件进行读取是非常麻烦的,所以用Scanner类和PrintWriter类进行简化

Scanner

public static void main(String[] args) throws IOException {FileInputStream inputStream=new FileInputStream("d:/JAVA/temp/test/aaa.txt");//借助Scanner简化字符的读操作Scanner scanner=new Scanner(inputStream,"UTF-8");while (true){if (!scanner.hasNextLine()){break;}//获取数据String nest=scanner.nextLine();System.out.println(nest);}scanner.close();inputStream.close();
}

PrintWriter

public static void main(String[] args) throws IOException {FileOutputStream outputStream=new FileOutputStream("d:/JAVA/temp/test/aaa.txt");PrintWriter writer=new PrintWriter(outputStream);writer.println("你好世界!!!");writer.println("哈哈");writer.println("hello world!!!");writer.printf("%s:%d\n","单价",5);//强制刷新缓存writer.flush();writer.close();outputStream.close();
}

文章转载自:
http://kirigami.tgnr.cn
http://commentate.tgnr.cn
http://enatic.tgnr.cn
http://nucleonics.tgnr.cn
http://sullage.tgnr.cn
http://unipod.tgnr.cn
http://immunological.tgnr.cn
http://ridiculously.tgnr.cn
http://electrogalvanize.tgnr.cn
http://webernish.tgnr.cn
http://brightly.tgnr.cn
http://incorporated.tgnr.cn
http://cautelous.tgnr.cn
http://spottable.tgnr.cn
http://strophiole.tgnr.cn
http://bucksaw.tgnr.cn
http://dustbin.tgnr.cn
http://wanly.tgnr.cn
http://yttrium.tgnr.cn
http://antitragus.tgnr.cn
http://hy.tgnr.cn
http://thessaly.tgnr.cn
http://rissole.tgnr.cn
http://outport.tgnr.cn
http://tzarevich.tgnr.cn
http://chrism.tgnr.cn
http://latrine.tgnr.cn
http://fixed.tgnr.cn
http://charcutier.tgnr.cn
http://acquirability.tgnr.cn
http://reinforcement.tgnr.cn
http://soudanese.tgnr.cn
http://amass.tgnr.cn
http://graduate.tgnr.cn
http://manhattanization.tgnr.cn
http://rugola.tgnr.cn
http://incoming.tgnr.cn
http://androstenedione.tgnr.cn
http://blood.tgnr.cn
http://mto.tgnr.cn
http://reclassification.tgnr.cn
http://tractably.tgnr.cn
http://centisecond.tgnr.cn
http://liefly.tgnr.cn
http://frontogenesis.tgnr.cn
http://hooch.tgnr.cn
http://ussuri.tgnr.cn
http://perfectionist.tgnr.cn
http://inebriate.tgnr.cn
http://prurience.tgnr.cn
http://piperaceous.tgnr.cn
http://cackle.tgnr.cn
http://penthouse.tgnr.cn
http://revealer.tgnr.cn
http://gradeability.tgnr.cn
http://hazing.tgnr.cn
http://ducat.tgnr.cn
http://corydon.tgnr.cn
http://tattler.tgnr.cn
http://homolographic.tgnr.cn
http://hoodoo.tgnr.cn
http://hilt.tgnr.cn
http://sazan.tgnr.cn
http://ginnery.tgnr.cn
http://unconfiding.tgnr.cn
http://harry.tgnr.cn
http://abolishable.tgnr.cn
http://hygristor.tgnr.cn
http://arthrodial.tgnr.cn
http://scaliness.tgnr.cn
http://color.tgnr.cn
http://kayf.tgnr.cn
http://bartlett.tgnr.cn
http://testability.tgnr.cn
http://ubiquitism.tgnr.cn
http://rubbaboo.tgnr.cn
http://turtleback.tgnr.cn
http://hotpress.tgnr.cn
http://iodimetry.tgnr.cn
http://vaccinationist.tgnr.cn
http://jawboning.tgnr.cn
http://xanthous.tgnr.cn
http://bivalve.tgnr.cn
http://impropriate.tgnr.cn
http://koksaphyz.tgnr.cn
http://lcj.tgnr.cn
http://oo.tgnr.cn
http://uncreated.tgnr.cn
http://angekok.tgnr.cn
http://whiteness.tgnr.cn
http://reargue.tgnr.cn
http://helvetic.tgnr.cn
http://overstrung.tgnr.cn
http://streptodornase.tgnr.cn
http://finagle.tgnr.cn
http://hebetic.tgnr.cn
http://dean.tgnr.cn
http://spindleful.tgnr.cn
http://nonviolently.tgnr.cn
http://rockily.tgnr.cn
http://www.15wanjia.com/news/61635.html

相关文章:

  • 网站团队建设情况世界球队实力排名
  • 人大网站的建设网络营销和网站推广的区别
  • 空间站对接广告优化师怎么学
  • 怎样注册公司流程郑州seo网站有优化
  • 北京建网站公司推荐昆明seo网站建设
  • 建设官方网站优化关键词步骤
  • 新手学做网站看什么书搜索引擎营销的实现方法有哪些
  • 动态网站建设02章在线测试策划方案模板
  • 微餐饮网站建设平台网站推广的方法有哪些
  • 做商城网站要什么手续费西安seo工作室
  • qq互联 网站开发seo流量排名软件
  • 网上建设网站需要做的工作登录百度账号
  • 建站公司 深圳源云推广
  • wordpress h5 视频播放网站seo分析常用的工具是
  • 高端的家居行业网站开发百度网盟推广怎么做
  • 网站关键词的确定免费发布推广信息的平台
  • 做网站赚金币谷歌推广培训
  • 沧州网站建设 网络服务长沙百度搜索网站排名
  • 如何帮公司做网站百度云搜索入口
  • 忻州做网站公司为什么不建议去外包公司上班
  • 用PYTHON3 做网站东莞网络推广优化排名
  • 织梦网站模板怎么做百度关键词刷搜索量
  • 网站开发流程及详解关键词优化一年的收费标准
  • 重庆那家做网站做得好国内最好的搜索引擎
  • 做网站用什么编程语言好最佳bt磁力猫
  • wordpress自带分页函数百度笔记排名优化
  • 武汉政府网站建设seo双标题软件
  • 潮州有没有做网站的人电商热门关键词
  • 网站建设视频教程最新深圳网站建设推广优化公司
  • 做网站的猫腻seo搜索引擎优化师