第一范文网 - 专业文章范例文档资料分享平台

Java语言实验6 常用类和输入输出流

来源:用户分享 时间:2025/7/23 15:09:15 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

if ((month == 4) || (month == 6) || (month == 9) || (month == 11)){ return 30; } if (month == 2) return isLeapYear(year) ? 29:28; return 0;//如果月份错误 } public static boolean isLeapYear(int year) { // return true; return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } } 运行结果: 4、字符输入流和字符输出流 (1)内容 编写一个Java应用程序,将已存在的扩展名为.txt文本文件加密后存入另一个文本文件中。 (2) 源代码: package t4; import java.io.*; public class SecretExample { public static void main(String args[ ]) {

File fileOne=new File(\), fileTwo=new File(\); char b[]=new char[100]; try{ FileReader in=new FileReader(fileOne);//创建指向fileOne的字符输入流 FileWriter out=new FileWriter(fileTwo);//创建指向fileTwo字符输出流。 int n=-1; while((n=in.read(b))!=-1) { for(int i=0;i

运行结果: 5、缓冲流 (1)内容 编写一个Java应用程序,给已存在的txt文本文件添加行号。要求该文本文件事先用文本编辑完毕,保存在C:\\1000目录中,命名为hello.txt。 (2) 源代码: package t5; import java.io.*; public class ReadExample { public static void main(String args[ ]) {

File file=new File(\); File tempFile=new File(\); try{ FileReader inOne=new FileReader(file);// 创建指向文件file的输入流。 BufferedReader inTwo= new BufferedReader(inOne);// 创建指向inOne file的输入流。 FileWriter tofile=new FileWriter(tempFile);// 创建指向文件tempFile的输出流。 BufferedWriter out=new BufferedWriter(tofile);// 创建指向tofile的输出流。 String s=null; int i=0; s=inTwo.readLine();//inTwo读取一行。 while(s!=null) { i++; out.write(i+\+s); out.newLine(); s=inTwo.readLine();//inTwo读取一行。 } inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close(); inOne=new FileReader(tempFile);// 创建指向文件tempFile的输入流 inTwo= new BufferedReader(inOne);// 创建指向inOne file的输入流。 tofile=new FileWriter(file);// 创建指向文件file的输出流。 out=new BufferedWriter(tofile);// 创建指向tofile的输出流。 while((s=inTwo.readLine())!=null) ////inTwo读取一行。 { out.write(s); out.newLine(); } inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close(); inOne=new FileReader(file);// 创建指向文件file的输入流。 inTwo=new BufferedReader(inOne);// 创建指向inOne file的输入流。 while((s=inTwo.readLine())!=null) ////inTwo读取一行。 { System.out.println(s); } inOne.close();

搜索更多关于: Java语言实验6 常用类和输入输出流 的文档
Java语言实验6 常用类和输入输出流.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2g2yc4n9mw565jb3urou8mpoj7ocb000zql_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top