news 2026/5/12 6:39:38

SDUT Java--输入输出

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SDUT Java--输入输出

8-1 sdut-IO-2 利用字节输入输出流类,写出数据并读取文件

package 复习; import java.io.*; import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); String filePath="d:\\data.dat"; File file=new File(filePath); file.createNewFile(); FileOutputStream fout=new FileOutputStream(filePath); String line=sc.nextLine(); while(!line.equals("end")) { fout.write(line.getBytes()); line=sc.nextLine(); } fout.close(); FileInputStream fin=new FileInputStream(filePath); int read=0; byte[]bytes=new byte[1024]; while((read=fin.read(bytes))!=-1) { System.out.println(new String(bytes,0,read)); } fin.close(); } }

8-2 sdut-IO-3 利用字符输入输出流类,写出数据并读取文件

package 复习; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); String filename="d:\\data.dat"; File f=new File(filename); f.createNewFile(); FileWriter fw=new FileWriter(filename); while(sc.hasNext()) { String s=sc.nextLine(); if("end".equals(s)) break; fw.write(s); } fw.close(); FileReader fr=new FileReader(filename); int read=0; char[] ch=new char[1024]; while((read=fr.read(ch))!=-1) { System.out.println(new String(ch,0,read)); } fr.close(); } }

8-3 sdut-IO-5 读取网络资源,写出代码至文件

package 复习; import java.io.*; import java.net.URL; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { URL url=new URL("https://www.sdut.edu.cn/"); InputStream is = url.openStream(); String filePath="d:\\stut.html"; FileOutputStream fos=new FileOutputStream(filePath); int read=0; byte[]bytes=new byte[1024]; while((read=is.read(bytes))!=-1) { fos.write(bytes,0,read); } } }

8-4 sdut-IO-1 使用Scanner类解析文件

package 复习; import java.io.*; import java.net.URL; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { String filePath="d:\\test.txt"; Scanner sc=new Scanner(new File(filePath)); StringBuffer ch=new StringBuffer(); StringBuffer dig=new StringBuffer(); while(sc.hasNext()) { String s=sc.nextLine(); for(char c:s.toCharArray()) { if(c>='0'&&c<='9') dig.append(ch); else if(c>='A'&&c<='Z'||c>='a'&&c<='z') ch.append(ch); } } System.out.println(ch.toString()); System.out.println(dig.toString()); } }

8-5 sdut-IO-4 利用对象输入/输出流进行文件的读写

package 复习; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException, ClassNotFoundException { Scanner sc = new Scanner(System.in); Student[] stuArray = { new Student(1, "lr", 19, "软件2402"), new Student(2, "john", 20, "nnnn") }; ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\data.txt")); oos.writeObject(stuArray); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\data.txt")); Object o = ois.readObject(); if (o instanceof Student[]) { System.out.println(o); } ois.close(); } public static class Student implements Serializable { private int id; private String name; private int age; private String className; public Student(int id, String name, int age, String className) { this.id = id; this.name = name; this.age = age; this.className = className; } } }

8-6 sdut-IO-6 利用DataInputStream和DataOutputStream读写文件

package 复习; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException, ClassNotFoundException { Scanner sc = new Scanner(System.in); DataInputStream din = new DataInputStream(new FileInputStream("D:\\data.txt")); DataOutputStream dout = new DataOutputStream(new FileOutputStream("D:\\data.txt")); for(int i=1;i<=100;i++) { dout.writeInt(i); } for(char i='a';i<='z';i++) { dout.writeChar(i); } for (char i = 'A'; i < 'Z'; i++) { dout.writeChar(i); } for (int i = 1; i <=100; i++) { System.out.println(din.readInt()); } for (char i = 'a'; i < 'z'; i++) { System.out.println(din.readChar()); } for (char i = 'A'; i < 'Z'; i++) { System.out.println(din.readChar()); } } }

8-7 sdut-IO-7 利用File类进行文件及文件夹名称的显示

package PTA; import java.io.File; public class Main { public static void main(String\[\] args) { File file = new File("D:\\"); File\[\] files = file.listFiles(); if (files == null) System.out.println("不存在"); else { for (int i = 0; i < files.length; i++) { System.out.println(files\[i\]); } String\[\] list =file.list(); for (int j = 0; j < list.length; j++) { System.out.println(list\[j\]); } } } }

8-8 sdut-IO-8 使用File类创建一个多层目录d:\java\ch10\src

package 复习; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException, ClassNotFoundException { File file=new File("d:\\myjava\\ch07\\src"); if(!file.exists()) { file.mkdirs(); } else System.out.println("已存在"); } }

8-9 sdut-IO-9 使用File类列出目录下修改日期晚于2022-01-01的目录和文件名称

package 复习; import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException, ClassNotFoundException, ParseException { Scanner sc = new Scanner(System.in); File file=new File("d:\\"); File[]files=file.listFiles(); String k="2022-01-01 00:00:00"; SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss"); Date date=sdf.parse(k); for(int i=0;i<files.length;i++) { long l=files[i].lastModified(); Date d=new Date(l); if(d.after(date)) { System.out.println(files[i]); } } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 6:26:21

在Windows上搭建专业级日志管理中心的完整指南

在Windows上搭建专业级日志管理中心的完整指南 【免费下载链接】visualsyslog Syslog Server for Windows with a graphical user interface 项目地址: https://gitcode.com/gh_mirrors/vi/visualsyslog 想要让网络设备日志管理变得简单高效吗&#xff1f;Visual Syslog…

作者头像 李华
网站建设 2026/5/5 7:11:11

Hotkey Detective:精准解决Windows热键冲突的专业利器

Hotkey Detective&#xff1a;精准解决Windows热键冲突的专业利器 【免费下载链接】hotkey-detective A small program for investigating stolen hotkeys under Windows 8 项目地址: https://gitcode.com/gh_mirrors/ho/hotkey-detective 在日常使用Windows系统时&…

作者头像 李华
网站建设 2026/5/7 2:34:34

ZerotierFix终极指南:解锁Android设备上的高级网络配置

你是否厌倦了传统网络连接的限制&#xff1f;想要在Android设备上获得更灵活、更强大的网络连接能力&#xff1f;ZerotierFix正是为你量身打造的解决方案。这个基于官方Zerotier客户端深度定制的Android应用&#xff0c;不仅保留了原版的所有功能&#xff0c;更添加了许多实用特…

作者头像 李华
网站建设 2026/5/1 11:39:47

PiliPlus:解锁B站第三方客户端的终极观影体验

PiliPlus&#xff1a;解锁B站第三方客户端的终极观影体验 【免费下载链接】PiliPlus PiliPlus 项目地址: https://gitcode.com/gh_mirrors/pi/PiliPlus 在众多视频平台中&#xff0c;B站以其独特的社区氛围和丰富的内容生态深受用户喜爱。然而官方客户端的某些限制让不少…

作者头像 李华
网站建设 2026/5/9 6:34:01

暗黑破坏神2单机革命:PlugY插件如何让你重新定义游戏体验

暗黑破坏神2单机革命&#xff1a;PlugY插件如何让你重新定义游戏体验 【免费下载链接】PlugY PlugY, The Survival Kit - Plug-in for Diablo II Lord of Destruction 项目地址: https://gitcode.com/gh_mirrors/pl/PlugY 还在为暗黑2单机模式的种种限制而烦恼吗&#x…

作者头像 李华