-
Notifications
You must be signed in to change notification settings - Fork 2
/
F.java
150 lines (141 loc) · 3.76 KB
/
F.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package qgb;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import qgb.*;
import qgb.annotation.tested;
import qgb.file.FileTypeFilter;
public class F {
public static void main(String[] args) throws IOException {
//InputStream is = U.read_bis("c.jsp");
// String str = "";
// str = ToHex(is);
// U.write("c.txt", str);
//U.print(Integer.valueOf());
byte[] yb=hexToBytes("3031323334353637");
U.print(new String(yb));
U.print(bytesToHex(yb));
// U.print(getFilesStringArray("d:/test/", ".txt"));
}
//////////////////////////////////////////////
/**
*Example1: getFilesStringArray("d:/",".txt");</p>
*Example2: getFilesStringArray("d:",".txt");</p>
*
* @see java.nio.charset.Charset
* @see qgb.T#write(String, InputStream)
* @see java.lang.String#getBytes()
*/
public static String[] getFilesStringArray(String filePath, String astFileType){
Object[] yo=getFilesStringList(filePath, astFileType).toArray();
String[] yst=new String[yo.length];
for (int i = 0; i < yst.length; i++) {
yst[i]=yo[i].toString().replace("\\", "/");
}
return yst;
}
public static ArrayList<String> getFilesStringList(String filePath, String astFileType) {
File root = new File(filePath);
File[] files = root.listFiles();
ArrayList<String> filelist = new ArrayList<String>();
String sta="";
for (File file : files) {
if (file.isDirectory()) {
/*
* 递归调用
*/
filelist.addAll(getFilesStringList(file.getAbsolutePath()
, astFileType));
} else {
sta=file.getAbsolutePath();
if (FileTypeFilter.accept(sta, astFileType)) {
filelist.add(sta);
}
}
}
return filelist;
}
// /////////////////////////////////////////////
public static byte[] hexToBytes(String ast) {
if (ast.length()%2!=0) {
U.notify("illegal input! |ast.length()="+ast.length());
return null;
}
byte[] yb =new byte[ast.length()/2];
String str;
int ia=0;
for (int i = 0; i < ast.length()/2; i++) {
str=ast.substring(i*2,i*2+2);
ia=Integer.valueOf(str, 16);
yb[i]=(byte)ia;
}
return yb;
}
public static String isToHex(InputStream ais) {
return bytesToHex(U.InputStreamToBytes(ais));
}
@tested("2015-04-26 00:06:30")
public static String bytesToHex(byte[] ayb) {
StringBuilder sb=new StringBuilder();
for (int i = 0; i < ayb.length; i++) {
//Byte b = ayb[i];
sb.append( byteToHex(ayb[i]));
}
return sb.toString().toUpperCase();
}
public static String byteToHex(Byte ab) {
if ( ab>=0&&ab<=0xf) {
return ("0"+Integer.toString(ab.intValue(), 16));
}else {
return (Integer.toString(F.byteToint(ab), 16));
}
}
public static String name(String ast) {
ast = ast.replaceAll("[\\\\\\/\\:\\*\\?\\\"\\<\\>\\|]", "");
ast = ast.substring(0, Math.min(ast.length(), 200));
while (ast.startsWith(".")) {
ast=ast.substring(1, ast.length());
}
//U.print(ast);
return ast;
}
/**不能使用File.isDirectory()判断一个不存在的文件是否为目录**/
public static boolean isDirectory(String asPath) {
if (asPath.endsWith("/")||asPath.endsWith("\\")) {
return true;
}
return false;
}
public static boolean isExist(String astF) {
astF=U.autoPath(astF);
return new File(astF).exists();
}
public static void rename(String sFP, String sFNOld, String sFNNew) {
rename(sFP+sFNOld, sFP+sFNNew);
}
public static void rename(String sFOld, String sFNew) {
File f=new File(sFOld);
if (!f.renameTo(new File(sFNew))) {
U.notify(sFOld+"\n"+sFNew);
}
}
public static byte intTobyte(int i) {
if (i<0||i>255) {
U.argsError(i);
}
if (i<0x80) {
return (byte) i;
}else {
return (byte)( i+256);
}
}
/**0-255*/
public static int byteToint(byte b) {
if (b>=0) {
return (int)b;
}else {
return (int)( b)+256;
}
}
}