Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8465138

Browse files
testing print percentage, working download
1 parent 94f61f1 commit 8465138

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

‎javademo/pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@
122122
<version>1.3</version>
123123
</dependency>
124124

125+
<!-- apache common io -->
126+
<dependency>
127+
<groupId>commons-io</groupId>
128+
<artifactId>commons-io</artifactId>
129+
<version>2.6</version>
130+
</dependency>
131+
132+
125133
<dependency>
126134
<groupId>com.hierynomus</groupId>
127135
<artifactId>sshj</artifactId>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package console;
2+
3+
import java.io.PrintStream;
4+
5+
/**
6+
* @author zacconding
7+
* @Date 2018年12月28日
8+
* @GitHub : https://github.com/zacscoding
9+
*/
10+
public class PercentagePrinter {
11+
12+
private final int width = 57;
13+
private PrintStream out;
14+
private String afterCompleteMessage;
15+
16+
public PercentagePrinter() {
17+
this(System.out, "Complete");
18+
}
19+
20+
public PercentagePrinter(PrintStream out, String afterCompleteMessage) {
21+
this.out = out;
22+
this.afterCompleteMessage = afterCompleteMessage;
23+
}
24+
25+
public void updateProgress(double progressPercentage) {
26+
if (progressPercentage < 0.0) {
27+
return;
28+
}
29+
30+
if (progressPercentage >= 1.0D) {
31+
complete();
32+
return;
33+
}
34+
35+
out.print("\r[");
36+
37+
int offset = 0;
38+
int size = (int) (progressPercentage * width);
39+
40+
while (offset <= size) {
41+
out.print('#');
42+
offset++;
43+
}
44+
45+
while (offset < width) {
46+
out.print(" ");
47+
offset++;
48+
}
49+
50+
String percentage = String.format("%.2f%%", (progressPercentage * 100));
51+
52+
out.printf("] %3s", percentage);
53+
}
54+
55+
public void complete() {
56+
out.println("\r" + afterCompleteMessage);
57+
}
58+
}

‎javademo/src/main/java/nio/file/watch/FileWatcher2.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public boolean regist(File file, FileListener fileListener) {
7777
}
7878
}
7979

80+
/**
81+
* remove to subscribe
82+
* @param file detected file or directory
83+
* @param id to distingush thread
84+
*/
8085
public void remove(File file, String id) {
8186
if (file != null && StringUtils.hasText(id)) {
8287
Path detectingPath = getPathOrParentPath(file);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package console;
2+
3+
/**
4+
* @author zacconding
5+
* @Date 2018年12月28日
6+
* @GitHub : https://github.com/zacscoding
7+
*/
8+
public class PercentagePrinterTest {
9+
10+
public static void main(String[] args) throws Exception {
11+
PercentagePrinter printer = new PercentagePrinter();
12+
13+
for (double progressPercentage = 0.0D; progressPercentage <= 1.0D; progressPercentage += 0.01D) {
14+
printer.updateProgress(progressPercentage);
15+
Thread.sleep(20);
16+
}
17+
printer.complete();
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package util;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.Proxy;
6+
import java.net.URL;
7+
import java.net.URLConnection;
8+
import org.apache.commons.io.FileUtils;
9+
10+
/**
11+
* @author zacconding
12+
* @Date 2018年12月28日
13+
* @GitHub : https://github.com/zacscoding
14+
*/
15+
public class UrlDownloadTest {
16+
17+
public static void main(String[] args) {
18+
19+
}
20+
21+
private static void copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout,
22+
Proxy proxy) throws IOException {
23+
24+
final URLConnection connection = proxy != null ? source.openConnection(proxy) : source.openConnection();
25+
connection.setConnectTimeout(connectionTimeout);
26+
connection.setReadTimeout(readTimeout);
27+
28+
FileUtils.copyInputStreamToFile(connection.getInputStream(), destination);
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /