1 package hello; 2 import java.util.*; 3 4 public class Hello { 5 public static void main(String args[]){ 6 double[] a = produce(10000); 7 int[] b = tongJi(a); 8 for (int e : b) { 9 System.out.print(e +" "); 10 } 11 System.out.println(); 12 double exp = expect(a); 13 System.out.println(exp); 14 double f = fangCha(exp, a); 15 System.out.println(f); 16 17 } 18 public static double[] produce(int length) { 19 double[] a = new double[length]; 20 for (int i = 0; i < length; i++) { 21 a[i] = Math.random() * 2 - 1; 22 } 23 return a; 24 } 25 public static int[] tongJi(double a[]) { 26 int[] b = new int[20]; 27 for (int i = 0; i < a.length; i++) { 28 for (int j = 0; j < b.length; j++) { 29 if (a[i] > -1 + 0.1 * j && a[i] < -1 + 0.1 * (j + 1)) { 30 b[j]++; 31 } 32 } 33 } 34 return b; 35 } 36 public static double expect(double b[]) { 37 double sum = 0; 38 for (int i = 0; i < b.length; i++) { 39 sum = sum + b[i]; 40 } 41 double exp = sum/10000; 42 return exp; 43 } 44 public static double fangCha(double exp, double a[]) { 45 double sum = 0; 46 for (int i = 0; i < a.length; i++) { 47 sum = sum + (a[i] - exp) * (a[i] - exp); 48 } 49 double f = Math.sqrt(sum/10000); 50 return f; 51 } 52 }
1 package hello; 2 import java.util.*; 3 4 public class Hello { 5 public static void main(String args[]){ 6 double[] a = produce(10000); 7 int[] b = tongJi(a); 8 for (int e : b) { 9 System.out.print(e +" "); 10 } 11 System.out.println(); 12 double exp = expect(a); 13 System.out.println(exp); 14 double f = fangCha(exp, a); 15 System.out.println(f); 16 17 } 18 public static double[] produce(int length) { 19 double[] a = new double[length]; 20 for (int i = 0; i < length; i++) { 21 a[i] = getNumRandom(); 22 } 23 return a; 24 } 25 public static int[] tongJi(double a[]) { 26 int[] b = new int[20]; 27 for (int i = 0; i < a.length; i++) { 28 for (int j = 0; j < b.length; j++) { 29 if (a[i] > -6 + 0.6 * j && a[i] < -6 + 0.6 * (j + 1)) { 30 b[j]++; 31 } 32 } 33 } 34 return b; 35 } 36 public static double expect(double b[]) { 37 double sum = 0; 38 for (int i = 0; i < b.length; i++) { 39 sum = sum + b[i]; 40 } 41 double exp = sum/10000; 42 return exp; 43 } 44 public static double fangCha(double exp, double a[]) { 45 double sum = 0; 46 for (int i = 0; i < a.length; i++) { 47 sum = sum + (a[i] - exp) * (a[i] - exp); 48 } 49 double f = Math.sqrt(sum/10000); 50 return f; 51 } 52 public static double getNumRandom() { 53 double sum = 0; 54 for (int i = 0 ; i < 12; i++) { 55 sum = sum + Math.random(); 56 } 57 return sum - 6; 58 } 59 }
1 import java.util.Scanner; 2 3 /** 4 * Created by 木子月月鸟 on 2017/5/24. 5 */ 6 public class Test { 7 public static void main(String[] args) { 8 System.out.println("请输入第一个字符串数组:"); 9 Scanner input = new Scanner(System.in); 10 String s1 = input.nextLine(); 11 System.out.println("请输入第二个字符串数组:"); 12 String s2 = input.nextLine(); 13 System.out.println("比较结果为:"+compareMinus(s1, s2)); 14 } 15 public static boolean compareMinus(String s1, String s2) { 16 boolean flag = true; 17 for (int i = 0; i < s1.length(); i++) { 18 char ch = s1.charAt(i); 19 if (ch == '-') { 20 if (s2.charAt(i) != '-') { 21 flag = false; 22 } 23 } 24 } 25 return flag; 26 } 27 }
Java文件操作:
①文件删除与创建,文件存在则删除,不存在则创建
1 import java.io.*; 2 import java.util.*; 3 4 /** 5 * Created by 木子月月鸟 on 2017/5/24. 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 File file = new File("C:\\Users\\木子月月鸟\\Desktop\\file.txt"); 10 if (file.exists()) { 11 file.delete(); 12 System.out.println("文件已经删除"); 13 } else { 14 try { 15 file.createNewFile(); 16 System.out.println("文件已经创建"); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 } 21 } 22 }
②获取文件信息,名字,长度,是否隐藏等
1 import java.io.*; 2 import java.util.*; 3 4 /** 5 * Created by 木子月月鸟 on 2017/5/24. 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 File file = new File("C:\\Users\\木子月月鸟\\Desktop\\file.txt"); 10 if (file.exists()) { 11 String name = file.getName(); 12 long length = file.length(); 13 boolean hidden = file.isHidden(); 14 System.out.println("文件名称是:"+ name); 15 System.out.println("文件长度是:"+ length); 16 System.out.println("该文件是隐藏文件么?"+ hidden); 17 } else { 18 System.out.println("该文件不存在!"); 19 } 20 } 21 }
③用FileInputStream和FileOutputStream对文件进行读写操作
1 import java.io.*; 2 import java.util.*; 3 4 /** 5 * Created by 木子月月鸟 on 2017/5/24. 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 File file = new File("C:\\Users\\木子月月鸟\\Desktop\\file.txt"); 10 try { 11 FileOutputStream out = new FileOutputStream(file); 12 byte[] buy = "我有一只小毛驴,我从来也不骑".getBytes(); 13 out.write(buy); 14 out.close(); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 } 18 try { 19 FileInputStream in = new FileInputStream(file); 20 byte[] by = new byte[1024]; 21 int len = in.read(by); 22 System.out.println("文件中的信息是: "+new String(by, 0 ,len)); 23 in.close(); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 } 28 }
④用FileReader、FileWriter、BufferedReader、BufferedWriter对文件进行读写操作
1 import java.io.*; 2 import java.util.*; 3 4 /** 5 * Created by 木子月月鸟 on 2017/5/24. 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 File file = new File("C:\\Users\\木子月月鸟\\Desktop\\file.txt"); 10 String content[] = {"好久不见","最近好吗","常常联系"}; 11 try { 12 FileWriter fileWriter = new FileWriter(file); 13 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 14 for (int i = 0; i < content.length; i++) { 15 bufferedWriter.write(content[i]); 16 bufferedWriter.newLine(); 17 } 18 bufferedWriter.close(); 19 fileWriter.close(); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 try { 24 FileReader fileReader = new FileReader(file); 25 BufferedReader bufferedReader = new BufferedReader(fileReader); 26 String string = null; 27 int i = 0; 28 while ((string = bufferedReader.readLine()) != null) { 29 i++; 30 System.out.println("第"+ i + "行:" + string); 31 } 32 bufferedReader.close(); 33 fileReader.close(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 38 } 39 }
⑤用FileReader、FileWriter、BufferedReader、BufferedWriter对文件复制
1 import java.io.*; 2 import java.util.*; 3 4 /** 5 * Created by 木子月月鸟 on 2017/5/24. 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 File source = new File("C:\\Users\\木子月月鸟\\Desktop\\source.txt"); 10 File target = new File("C:\\Users\\木子月月鸟\\Desktop\\target.txt"); 11 try { 12 FileReader fileReader = new FileReader(source); 13 FileWriter fileWriter = new FileWriter(target); 14 BufferedReader bufferedReader = new BufferedReader(fileReader); 15 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 16 String string = null; 17 while ((string = bufferedReader.readLine()) != null) { 18 System.out.println(string); 19 bufferedWriter.write(string); 20 bufferedWriter.newLine(); 21 } 22 bufferedReader.close(); 23 fileReader.close(); 24 bufferedWriter.close(); 25 fileWriter.close(); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 } 30 }
⑥文件压缩操作
1 import java.io.*; 2 import java.util.*; 3 import java.util.zip.ZipEntry; 4 import java.util.zip.ZipOutputStream; 5 6 /** 7 * Created by 木子月月鸟 on 2017/5/24. 8 */ 9 public class Test { 10 public static void main(String[] args) { 11 Test test = new Test(); 12 try { 13 test.zip("C:\\Users\\木子月月鸟\\Desktop\\test.zip", new File("C:\\Users\\木子月月鸟\\Desktop\\test")); 14 System.out.println("压缩完成!"); 15 }catch (Exception e) { 16 } 17 } 18 private void zip(String zipFileName, File inputFile) throws Exception { 19 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); 20 zip(out, inputFile, ""); 21 System.out.println("文件压缩中....."); 22 out.close(); 23 } 24 private void zip(ZipOutputStream out, File file, String base) throws Exception { 25 if (file.isDirectory()) { 26 File[] files = file.listFiles(); 27 out.putNextEntry(new ZipEntry(base + "/")); 28 base = base.length() == 0 ? "" : base + "/"; 29 for (int i = 0; i < files.length; i++) { 30 zip(out, files[i], base + files[i]); 31 } 32 } else { 33 out.putNextEntry(new ZipEntry(base)); 34 FileInputStream in = new FileInputStream(file); 35 int b; 36 System.out.println(base); 37 while ((b = in.read()) != -1) { 38 out.write(b); 39 } 40 in.close(); 41 } 42 } 43 }
(7)文件解压缩操作