import java.util.ArrayList; import java.util.List; public class CollectionExample { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("C++"); list.remove("Python"); for (String language : list) { System.out.println(language); } } } public class StringExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; String result = str1 + ", " + str2; System.out.println(result); String subStr = str1.substring(1, 3); System.out.println(subStr); String newStr = str2.replace("World", "Java"); System.out.println(newStr); } } import java.util.Date; import java.text.SimpleDateFormat; public class DateTimeExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = format.format(date); System.out.println("Formatted Date: " + formattedDate); Date anotherDate = new Date(2021, 10, 1); long diff = date.getTime() - anotherDate.getTime(); long days = diff / (1000 * 60 * 60 * 24); System.out.println("Days Difference: " + days); } } import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class IOExample { public static void main(String[] args) { try { File file = new File("example.txt"); FileOutputStream outputStream = new FileOutputStream(file); outputStream.write("Hello, Java!".getBytes()); outputStream.close(); byte[] buffer = new byte[1024]; FileInputStream inputStream = new FileInputStream(file); int length = inputStream.read(buffer); String content = new String(buffer, 0, length); inputStream.close(); System.out.println("File Content: " + content); } catch (IOException e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文