java基础第20课

网友投稿 230 2022-11-22

java基础第20课

work097.javapackage test05;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;public class work097{ public static void main(String[] args) { List list1 = new ArrayList(); list1.add("A"); list1.add("a"); list1.add("c"); list1.add("C"); list1.add("a"); for (int i = 0; i < list1.size(); i++) { System.out.print(list1.get(i) + " "); } System.out.println(); Set set1 = new HashSet<>(); set1.add("A"); set1.add("a"); set1.add("c"); set1.add("C"); set1.add("a"); //set不接受重复值 Iterator iterator1 = set1.iterator(); while (iterator1.hasNext()) { String temp = (String) iterator1.next(); System.out.print(temp + " "); } System.out.println(); }}work098.javapackage test05;import java.io.File;public class work098{ public static void main(String[] args) { File file1 = new File("work.txt"); if(file1.exists()) { file1.delete(); System.out.println("文件已删除"); } else { try { file1.createNewFile(); System.out.println("文件已创建"); } catch (Exception e) { e.printStackTrace(); } } }}work099.javapackage test05;import java.io.File;public class work099{ public static void main(String[] args) { File file1 = new File("work.txt"); if (file1.exists()) { String name = file1.getName(); long length = file1.length(); boolean hidden = file1.isHidden(); System.out.println("文件名称:" + name); System.out.println("文件长度是:" + length); System.out.println("文件是否隐藏:" + hidden); } else { work099.createFile(); } } public static void createFile() { File file1 = new File("work.txt"); if(file1.exists()) { file1.delete(); System.out.println("文件已删除"); } else { try { file1.createNewFile(); System.out.println("文件已创建"); } catch (Exception e) { e.printStackTrace(); } } }}work100.javapackage test05;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class work100{public static void main(String[] args){File file1 = new File("work.txt");if (file1.exists()) {try {FileOutputStream out1 = new FileOutputStream(file1);String str1 = "我正在学习java编程";byte[] contents = str1.getBytes();out1.write(contents);out1.close(); } catch (Exception e) {e.printStackTrace(); }try {FileInputStream input1 = new FileInputStream(file1);byte contents[] = new byte[1024];int length = input1.read(contents);System.out.println("文件中的信息是:" + new String(contents,0,length));input1.close(); } catch (Exception e) {e.printStackTrace(); } } else {work100.createFile(); } }//创建文件public static void createFile(){File file1 = new File("work.txt");if(file1.exists()) {file1.delete();System.out.println("文件已删除"); }else {try {file1.createNewFile();System.out.println("文件已创建"); } catch (Exception e) {e.printStackTrace(); } } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:CDMA与LTE数据互操作的空中接口的讨论与分析
下一篇:脑机接口时代即将来临?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~