-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTransfer.java
More file actions
81 lines (76 loc) · 2.58 KB
/
FileTransfer.java
File metadata and controls
81 lines (76 loc) · 2.58 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
import java.io.*;
import java.math.BigInteger;
public class FileTransfer {
public String DoubleToBinary(String file) throws IOException{
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String out = file + "_Binary";
File output = new File(out);
if (!output.exists()) {
output.createNewFile();
}
FileWriter fw = new FileWriter(output.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
String line;
while((line = br.readLine()) != null){
double d = Double.valueOf(line);
String text = Long.toBinaryString(Double.doubleToRawLongBits(d));
bw.write(text);
}
in.close();
bw.close();
return out;
}
// index of the number in raw file, start from 0
public String BinaryToDouble(String file, int startIndex, int endIndex) throws IOException{
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String out = file + "_Normal";
File output = new File(out);
if (!output.exists()) {
output.createNewFile();
}
FileWriter fw = new FileWriter(output.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
int c;
int counter_bit = 0;
int counter_num = 0;
StringBuilder line = new StringBuilder();
while ((c = br.read()) != -1) {
if (counter_bit < startIndex * 62) {
counter_bit++;
continue;
} else if (counter_bit >= (endIndex + 1) * 62) {
break;
}
if(counter_num == 0) line = new StringBuilder();
line.append( (char)c );
if(counter_num == 61){
String result = line.toString();
double d = Double.longBitsToDouble(new BigInteger(result, 2).longValue());
bw.write(Double.toString(d) + '\n');
counter_num = -1;
}
counter_num ++;
counter_bit ++;
}
in.close();
bw.close();
return out;
}
public static void main(String[] args) throws IOException{
// double d = 1.0;
// String text = Long.toBinaryString(Double.doubleToRawLongBits(d));
// System.out.println(text.length());
// System.out.println("0b "+ text);
// double nd = Double.longBitsToDouble(new BigInteger(text, 2).longValue());
// System.out.println("double "+nd);
int startIndex = 3;
int endIndex = 5;
FileTransfer s = new FileTransfer();
String input = "svd.test.data.txt";
String binaryFile = s.DoubleToBinary(input);
String normalFile = s.BinaryToDouble(binaryFile, startIndex, endIndex);
// System.out.println(Double.doubleToRawLongBits(d));
}
}