forked from algorithm016-algorithm016/algorithm016
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
128 lines (111 loc) · 2.69 KB
/
Utils.java
File metadata and controls
128 lines (111 loc) · 2.69 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package src.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class Utils {
public static int[] arr = {1,8,4,3,4,6,8,9,33,5,6,2,5,753};
public static <T> T[] swap(T t1, T t2){
return null;
}
/*
角标
*/
public static void swap(int[] arr,int t1, int t2){
int temp = arr[t2];
arr[t2] = arr[t1];
arr[t1] = temp;
}
/**
* 产生一个size大小的、数组分布范围为0-range的数组
* @param size
* @param range
* @return
*/
public static int[] createRandomArr(int size,int range,boolean hasFeed){
Random r;
if (hasFeed) {
r = new Random(100l);
}else{
r = new Random();
}
int[] arr = new int[size];
for(int j =0; j<size;j++){
arr[j] = r.nextInt(range);
}
return arr;
}
public static int[] createRandomArr(int size,int range){
return createRandomArr(size,range,false);
}
public static int[] createNearlyOrderArr(int size,int range,boolean flag){
int[] arr = new int[size];
for(int j =0; j<size;j++){
arr[j] = j;
}
return arr;
}
public static int[] createNearlyOrderArr(int size,int range){
return createNearlyOrderArr(size,range,false);
}
public static String printIntSZ(int[] i){
StringBuffer sb = new StringBuffer();
for (int j = 0; j < i.length; j++) {
sb.append(i[j]).append(",");
}
sb.delete(sb.lastIndexOf(","), sb.length());
return sb.toString();
}
//字符串写到文件中
public static void writeToFile(String filePath,String fileName,Object something){
if (!filePath.endsWith("/")) {
filePath = filePath+"/";
}
FileOutputStream out = null;
PrintWriter pw = null;
try {
File file = new File(filePath);
if (!file.exists() ||!file.isDirectory()) {
file.mkdirs();
}
File file2 = new File(filePath+fileName);
/* out = new FileOutputStream(file2);
out.write(something.getBytes());
System.out.println("写入成功:"+filePath+fileName);*/
FileWriter fileWriter = new FileWriter(file2, true);
pw = new PrintWriter(fileWriter);
pw.println(something);
//pw.println("\n");
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (pw != null) {
try {
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
writeToFile("D:\\immoc.test", "sunfa.txt", "6666666666666");
}
public static int[] copyArr(int[] arr) {
int[] n = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
n[i]=arr[i];
}
return n;
}
}