Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 246: work with files example - bad examples, I'm sorry #202

Merged
merged 1 commit into from Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/com/demotivirus/Day_246/FindInBigFile.java
@@ -0,0 +1,43 @@
package com.demotivirus.Day_246;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;

public class FindInBigFile {
public static void main(String[] args) throws IOException {
Path path = Path.of("src/main/java/com/demotivirus/Day_246/chinese.txt");
System.out.println(countOccurrences(path, "11"));
System.out.println(countOccurrences(path, "22"));
}

private static int countOccurrences(Path path, String text) {
return countOccurrences(path, text, StandardCharsets.UTF_16);
}

private static int countOccurrences(Path path, String text, Charset charset) {
if (charset == null)
charset = StandardCharsets.UTF_16;

int count = 0;

try (BufferedReader br = Files.newBufferedReader(path, charset)) {
String line;
while ((line = br.readLine()) != null) { //not work correctly
count += countStringInInt(line, text);
}
} catch (IOException e) {
e.printStackTrace();
}

return count;
}

private static int countStringInInt(String string, String toFind) {
return string.split(Pattern.quote(toFind), -1).length - 1;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/demotivirus/Day_246/ReadBigFileExample.java
@@ -0,0 +1,32 @@
package com.demotivirus.Day_246;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;

public class ReadBigFileExample {
public static void main(String[] args) throws IOException {
Path chinese = Path.of("src/main/java/com/demotivirus/Day_246/chinese.txt");
// List<String> allLines = Files.readAllLines(chinese, StandardCharsets.UTF_16);
// allLines.forEach(System.out::println);

int MAP_SIZE = 524880; //5 MB
try (FileChannel fileChannel = (FileChannel.open(chinese, EnumSet.of(StandardOpenOption.READ)))) {
int position = 0;
long length = fileChannel.size();

while (position < length) {
long remaining = length - position;
int bytesToMap = (int) Math.min(MAP_SIZE, remaining);

MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, position, bytesToMap);
//to do

position += bytesToMap;
}
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/demotivirus/Day_246/ReadBinaryFiles.java
@@ -0,0 +1,26 @@
package com.demotivirus.Day_246;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

public class ReadBinaryFiles {
public static void main(String[] args) throws IOException {
Path binaryFile = Paths.get("src/main/java/com/demotivirus/Day_246/ReadBigFileExample.java");
int fileSize = (int) Files.readAttributes(binaryFile, BasicFileAttributes.class).size();

final byte[] buffer = new byte[fileSize];

try (InputStream is = new FileInputStream(binaryFile.toString())) {
int i;
while ((i = is.read(buffer)) != -1) {
System.out.println("Reading...");
System.out.println(is.read());
}
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/demotivirus/Day_246/WriteBinaryFile.java
@@ -0,0 +1,23 @@
package com.demotivirus.Day_246;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;

public class WriteBinaryFile {
public static void main(String[] args) throws IOException {
Path binaryFile = Paths.get("src/main/java/com/demotivirus/Day_246/ReadBigFileExample.java");
int fileSize = (int) Files.readAttributes(binaryFile, BasicFileAttributes.class).size();

final byte[] buffer = new byte[fileSize];

BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
Files.newOutputStream(binaryFile, StandardOpenOption.CREATE,
StandardOpenOption.WRITE));
bufferedOutputStream.write(buffer);
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/demotivirus/Day_246/WriteTextFile.java
@@ -0,0 +1,26 @@
package com.demotivirus.Day_246;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WriteTextFile {
private static Logger LOGGER = Logger.getLogger(WriteTextFile.class.getName());
public static void main(String[] args) {
Path textFile = Paths.get("written-file.txt");

try (BufferedWriter bw = Files.newBufferedWriter(textFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
bw.write("Code everyday for be harder");
bw.newLine();
bw.write("Repeat ten if you not understand");
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Something wos wrong {}", ex.getMessage());
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/demotivirus/Day_246/chinese.txt
@@ -0,0 +1,14 @@
你好世界中文
你好世界中文
你好世界中文
你好世界中文
你好世界中文
你好世界中文
你好世界中文
你好世界中文
你好世界中文
你好世界中文
111111
222
22
22
2 changes: 2 additions & 0 deletions written-file.txt
@@ -0,0 +1,2 @@
Code everyday for be harder
Repeat ten if you not understand