分布式系统
从txt文件中读数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class test { public static String txt2String(File file){ StringBuilder result = new StringBuilder(); try{ BufferedReader br = new BufferedReader(new FileReader(file)); String s = null; while((s = br.readLine())!=null){ result.append(System.lineSeparator()+s); } br.close(); }catch(Exception e){ e.printStackTrace(); } return result.toString(); } public static void main(String[] args){ File file = new File("book.txt"); System.out.println(txt2String(file)); } }
|
从文件中写数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.io.File; import java.io.FileWriter; import java.io.IOException; public class test { public static void main(String[] args) { File file = null; FileWriter fw = null; file = new File("test123.txt"); try { if (!file.exists()) { file.createNewFile(); } fw = new FileWriter(file); for(int i = 1;i <=3000;i++){ fw.write("abcdefgabcdefg"+i+","); fw.write("sssssssssssssss"+i+",\r\n"); fw.flush(); } System.out.println("write data success ful!"); } catch (IOException e) { e.printStackTrace(); }finally{ if(fw != null){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|
从命令行中读数据
1 2 3 4 5 6 7 8 9 10 11
| Scanner cin=new Scanner(System.in); int T=cin.nextInt(); String str = "1998.07.01"; String[] s = str.split("\\."); String year = s[0]; String month = s[1]; String day = s[2]; System.out.println(year); System.out.println(month); System.out.println(day);
|
各类型的数字和字符串之间的转化
1 2 3 4 5 6 7 8 9 10 11
| String s = "100"; int a = Integer.parseInt(String s); Long.parseLong(String s); Float.parseFloat(String s); Double.parseDouble(String s) int i=11; String s=Integer.toString(i);
|
支持中文的编译
1
| javac -encoding utf-8 BarChart.java
|
未解决的问题