面试时间: 2019-4-19
- byte b = (byte) 128;
答:b为-128
- byte a = (byte) 127; 127
- byte b = (byte) 128; -128
- byte c = (byte) 129; -127
- byte d = (byte) 130; -126
使用两种方法遍历map集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class Demo{
public static void main(String[] args) {
Map<Integer, Integer> map = new HashedMap<Integer, Integer>() {
{
put(1, 1);
put(2, 2);
put(3, 3);
}
};
for (Integer key : map.keySet()) {
System.out.println(map.get(key));
}
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}将
d:\\src
下的a.txt文件复制到e:\\src
下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
37public class CopyFile {
public static void main(String[] args) {
// 方式一
File file = new File("E:/src/a.mp4");
File dir = new File("D:/src/");
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new RuntimeException("文件夹创建失败");
} else {
if (file.renameTo(new File("D:/src/a.mp4"))) {
System.out.println("移动完成!");
} else {
System.out.println("移动失败!");
}
}
}
// 方式二
Path path1 = Paths.get("D:/src/a.mp4");
Path path2 = Paths.get("E:/src/a.mp4");
Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
// 方式三
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
try (FileChannel inChannel = FileChannel.open(Paths.get("D:/src/a.mp4"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("E:/src/a.mp4"), StandardOpenOption.CREATE, StandardOpenOption.WRITE)
) {
while (inChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}