回答編集履歴

11

tempの型をintに

2018/01/09 02:26

投稿

tignear
tignear

スコア260

test CHANGED
@@ -108,7 +108,7 @@
108
108
 
109
109
  public int compareTo(PathAndModifiedTime o){
110
110
 
111
- long temp=Long.compare(t,o.t);
111
+ int temp=Long.compare(t,o.t);
112
112
 
113
113
  if(temp!=0){
114
114
 

10

バグ修正

2018/01/09 02:26

投稿

tignear
tignear

スコア260

test CHANGED
@@ -116,7 +116,7 @@
116
116
 
117
117
  }
118
118
 
119
- return t.p.compareTo(o.p);
119
+ return p.compareTo(o.p);
120
120
 
121
121
  }
122
122
 

9

()抜けを修正 更新日時が同じ場合パスの大小を用いるように修正

2018/01/09 00:23

投稿

tignear
tignear

スコア260

test CHANGED
@@ -98,7 +98,7 @@
98
98
 
99
99
  }
100
100
 
101
- public Path getPath{
101
+ public Path getPath(){
102
102
 
103
103
  return p;
104
104
 
@@ -108,7 +108,15 @@
108
108
 
109
109
  public int compareTo(PathAndModifiedTime o){
110
110
 
111
+ long temp=Long.compare(t,o.t);
112
+
113
+ if(temp!=0){
114
+
115
+ return temp;
116
+
117
+ }
118
+
111
- return Long.compare(t,o.t);
119
+ return t.p.compareTo(o.p);
112
120
 
113
121
  }
114
122
 

8

メソッドの勘違いを修正

2018/01/08 14:10

投稿

tignear
tignear

スコア260

test CHANGED
@@ -106,9 +106,9 @@
106
106
 
107
107
  @Override
108
108
 
109
- public int compare(PathAndModifiedTime f,PathAndModifiedTime s){
109
+ public int compareTo(PathAndModifiedTime o){
110
110
 
111
- return Long.compare(f.t,s.t);
111
+ return Long.compare(t,o.t);
112
112
 
113
113
  }
114
114
 

7

型間違いを修正

2018/01/02 18:50

投稿

tignear
tignear

スコア260

test CHANGED
@@ -78,7 +78,7 @@
78
78
 
79
79
  ```java
80
80
 
81
- public class PathAndModifiedTime implements Comparable<PathAndmodifedTime>{
81
+ public class PathAndModifiedTime implements Comparable<PathAndModifiedTime>{
82
82
 
83
83
  private Path p;
84
84
 

6

型が一致してないのを修正

2018/01/02 18:10

投稿

tignear
tignear

スコア260

test CHANGED
@@ -64,7 +64,7 @@
64
64
 
65
65
  }
66
66
 
67
- }).collect(Collectors.toCollection(TreeMap::new));
67
+ }).collect(Collectors.toCollection(TreeSet::new));
68
68
 
69
69
  }catch(UncheckedIOException ex){
70
70
 

5

セミコロン追加

2018/01/02 18:07

投稿

tignear
tignear

スコア260

test CHANGED
@@ -100,7 +100,7 @@
100
100
 
101
101
  public Path getPath{
102
102
 
103
- return p
103
+ return p;
104
104
 
105
105
  }
106
106
 

4

返り値ミスを修正

2018/01/02 18:00

投稿

tignear
tignear

スコア260

test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  ```java
48
48
 
49
- TreeSet<PathAndModifiedTime> void test() throws IOException{
49
+ TreeSet<PathAndModifiedTime> test() throws IOException{
50
50
 
51
51
  try(Stream<Path> s=Files.walk(Paths.get("."))){
52
52
 
@@ -54,7 +54,7 @@
54
54
 
55
55
  try{
56
56
 
57
- long t=Files.getLastModifiedTime(p);
57
+ long t=Files.getLastModifiedTime(p);
58
58
 
59
59
  return new PathAndModifiedTime(p,t);
60
60
 

3

並べ替えるコードも追加

2018/01/02 17:57

投稿

tignear
tignear

スコア260

test CHANGED
@@ -2,21 +2,35 @@
2
2
 
3
3
  ```java
4
4
 
5
+ try{
5
6
 
7
+ try(Stream<Path> s=Files.find(Paths.get("."),1,(p,e)->System.currentTimeMillis()-e.lastModifiedTime().toMillis()>TimeUnit.MILLISECONDS.convert(5L,TimeUnit.DAYS))){//並べ替えなくてもできた(1)(2)
6
8
 
7
- try(Stream s=Files.find(Paths.get("."),1,(p,e)->System.currentTimeMillis()-e.lastModifiedTime().toMillis()>TimeUnit.MILLISECONDS.convert(5L,TimeUnit.DAYS))){//並べ替えなくてもできた(1)(2)
9
+ s.parallel().forEach(p->{
8
10
 
9
- s.parallel().forEach(p->{
11
+ try{
10
12
 
11
- Files.deleteIfExists(p);
13
+ Files.deleteIfExists(p);
12
14
 
13
- System.out.println(p);//ログ出力(3)
15
+ System.out.println(p);//ログ出力(3)
14
16
 
17
+ }catch(IOException ex){
18
+
19
+ throw new UncheckedIOException(ex);
20
+
21
+ }
22
+
15
- });
23
+ });
24
+
25
+ }catch(UncheckedIOException ex){
26
+
27
+ throw ex;
28
+
29
+ }
16
30
 
17
31
  }catch(IOException ex){
18
32
 
19
- System.out.println(ex);
33
+ System.out.println(ex);
20
34
 
21
35
  }
22
36
 
@@ -26,4 +40,80 @@
26
40
 
27
41
  並び替えが必要ならstream apiでも使って並べてください
28
42
 
43
+ よく考えたら並び替えには最終更新日時が必要ですよね
44
+
45
+ 二回ファイルの属性を取得するのは無駄なので……
46
+
47
+ ```java
48
+
49
+ TreeSet<PathAndModifiedTime> void test() throws IOException{
50
+
51
+ try(Stream<Path> s=Files.walk(Paths.get("."))){
52
+
53
+ return s.map(p->{
54
+
55
+ try{
56
+
57
+ long t=Files.getLastModifiedTime(p);
58
+
59
+ return new PathAndModifiedTime(p,t);
60
+
61
+ }catch(IOExceotion ex){
62
+
63
+ throw new UncheckedIOException(ex);
64
+
65
+ }
66
+
67
+ }).collect(Collectors.toCollection(TreeMap::new));
68
+
69
+ }catch(UncheckedIOException ex){
70
+
71
+ throw ex.getCause();
72
+
73
+ }
74
+
75
+ }
76
+
77
+ ```
78
+
79
+ ```java
80
+
81
+ public class PathAndModifiedTime implements Comparable<PathAndmodifedTime>{
82
+
83
+ private Path p;
84
+
85
+ private long t;
86
+
87
+ public PathAndModifiedTime(Path path,long time){
88
+
89
+ p=path;
90
+
91
+ t=time;
92
+
93
+ }
94
+
95
+ public long getModifiedTime(){
96
+
97
+ return t;
98
+
99
+ }
100
+
101
+ public Path getPath{
102
+
103
+ return p
104
+
105
+ }
106
+
107
+ @Override
108
+
109
+ public int compare(PathAndModifiedTime f,PathAndModifiedTime s){
110
+
111
+ return Long.compare(f.t,s.t);
112
+
113
+ }
114
+
115
+ }
116
+
117
+ ```
118
+
29
119
  コンパイル通してないんでよろしく

2

パフォーマンスがいいらしいdeleteIfExistsに変更

2018/01/02 17:56

投稿

tignear
tignear

スコア260

test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  s.parallel().forEach(p->{
10
10
 
11
- Files.delete(p);
11
+ Files.deleteIfExists(p);
12
12
 
13
13
  System.out.println(p);//ログ出力(3)
14
14
 

1

式の間違いを修正

2018/01/02 16:03

投稿

tignear
tignear

スコア260

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- try(Stream s=Files.find(Paths.get("."),1,(p,e)->e.lastModifiedTime().toMillis()-System.currentTimeMillis()>TimeUnit.MILLISECONDS.convert(5L,TimeUnit.DAYS))){//並べ替えなくてもできた(1)(2)
7
+ try(Stream s=Files.find(Paths.get("."),1,(p,e)->System.currentTimeMillis()-e.lastModifiedTime().toMillis()>TimeUnit.MILLISECONDS.convert(5L,TimeUnit.DAYS))){//並べ替えなくてもできた(1)(2)
8
8
 
9
9
  s.parallel().forEach(p->{
10
10