Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
22
Task/Read-entire-file/Java/read-entire-file-1.java
Normal file
22
Task/Read-entire-file/Java/read-entire-file-1.java
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReadFile {
|
||||
public static void main(String[] args) throws IOException{
|
||||
String fileContents = readEntireFile("./foo.txt");
|
||||
}
|
||||
|
||||
private static String readEntireFile(String filename) throws IOException {
|
||||
FileReader in = new FileReader(filename);
|
||||
StringBuilder contents = new StringBuilder();
|
||||
char[] buffer = new char[4096];
|
||||
int read = 0;
|
||||
do {
|
||||
contents.append(buffer, 0, read);
|
||||
read = in.read(buffer);
|
||||
} while (read >= 0);
|
||||
in.close();
|
||||
return contents.toString();
|
||||
}
|
||||
}
|
||||
20
Task/Read-entire-file/Java/read-entire-file-2.java
Normal file
20
Task/Read-entire-file/Java/read-entire-file-2.java
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import java.nio.channels.FileChannel.MapMode;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
|
||||
public class MMapReadFile {
|
||||
public static void main(String[] args) throws IOException {
|
||||
MappedByteBuffer buff = getBufferFor(new File(args[0]));
|
||||
String results = new String(buff.asCharBuffer());
|
||||
}
|
||||
|
||||
public static MappedByteBuffer getBufferFor(File f) throws IOException {
|
||||
RandomAccessFile file = new RandomAccessFile(f, "r");
|
||||
|
||||
MappedByteBuffer buffer = file.getChannel().map(MapMode.READ_ONLY, 0, f.length());
|
||||
file.close();
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
1
Task/Read-entire-file/Java/read-entire-file-3.java
Normal file
1
Task/Read-entire-file/Java/read-entire-file-3.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
String content = new Scanner(new File("foo"), "UTF-8").useDelimiter("\\A").next();
|
||||
15
Task/Read-entire-file/Java/read-entire-file-4.java
Normal file
15
Task/Read-entire-file/Java/read-entire-file-4.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import java.util.List;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.*;
|
||||
|
||||
public class ReadAll {
|
||||
public static List<String> readAllLines(String filesname){
|
||||
Path file = Paths.get(filename);
|
||||
return Files.readAllLines(file, Charset.defaultCharset());
|
||||
}
|
||||
|
||||
public static byte[] readAllBytes(String filename){
|
||||
Path file = Paths.get(filename);
|
||||
return Files.readAllBytes(file);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue