-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
executable file
·86 lines (80 loc) · 1.95 KB
/
Util.java
File metadata and controls
executable file
·86 lines (80 loc) · 1.95 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
import java.io.*;
import java.util.*;
public class Util
{
private static Map<String, BufferedReader> files = new HashMap<String, BufferedReader>();
private static Map<String, String> lines = new HashMap<String, String>();
private static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public static String readLine(String fileName)
{
try
{
if (!files.containsKey(fileName))
files.put(fileName, new BufferedReader(new FileReader(fileName)));
String line = lines.get(fileName);
if (line == null)
line = files.get(fileName).readLine();
lines.remove(fileName);
return line;
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
public static boolean hasMoreLines(String fileName)
{
try
{
if (!files.containsKey(fileName))
files.put(fileName, new BufferedReader(new FileReader(fileName)));
String line = lines.get(fileName);
if (line == null)
{
line = files.get(fileName).readLine();
lines.put(fileName, line);
}
return line != null;
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
public static void close(String fileName)
{
try
{
if (!files.containsKey(fileName))
throw new RuntimeException("File " + fileName + " is not open");
files.get(fileName).close();
files.remove(fileName);
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
public static String input()
{
try
{
return input.readLine();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
public static void pause(double seconds)
{
try
{
Thread.sleep((int)(seconds * 1000));
}
catch(InterruptedException e)
{
throw new RuntimeException(e);
}
}
}