Skip to content

Commit 61d3d0f

Browse files
committed
[add] add FileChannelTest for NIO
1 parent 0630b8b commit 61d3d0f

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.brianway.java.nio.tutorial;
2+
3+
import com.brianway.learning.java.nio.tutorial.BufferDemo;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
import java.io.RandomAccessFile;
9+
import java.nio.channels.FileChannel;
10+
11+
/**
12+
* FileChannel测试
13+
*
14+
* @auther brian
15+
* @since 2019/6/24 23:00
16+
*/
17+
public class FileChannelTest {
18+
private String dir = BufferDemo.class.getResource("/").getPath();
19+
private String path = BufferDemo.class.getResource("/").getPath() + "fileChannel.txt";
20+
21+
@Test
22+
public void testTruncate() throws IOException {
23+
RandomAccessFile aFile = new RandomAccessFile(dir + "truncate.txt", "rw");
24+
FileChannel channel = aFile.getChannel();
25+
int size = 10;
26+
// Truncates this channel's file to the given size.
27+
channel.truncate(size);
28+
long fileSize = channel.size();
29+
Assert.assertEquals(size, fileSize);
30+
}
31+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
asdasdas
2+
ahfsadkjhsdfkjl
3+
23232323
4+
2323
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
asdasdas
2+
ahfsadkjhsdfkjl
3+
23232323
4+
2323

0 commit comments

Comments
 (0)