Files
prog-intro/java/md2html/Md2Html.java

83 lines
2.7 KiB
Java

package md2html;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import markup.*;
public class Md2Html {
public static void main(String[] args) {
String text = readFile(args[0]);
BlockCreator creator = new BlockCreator(text);
List<String> blocks = creator.divideByBlocks();
try (
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(args[1]),
StandardCharsets.UTF_8
)
)
) {
for (int i = 0; i < blocks.size(); i++) {
PrimePartCreator creatorPrime = new PrimePartCreator(
blocks.get(i)
);
PrimePart part = creatorPrime.createPart();
StringBuilder sb = new StringBuilder();
part.toHtml(sb);
writer.write(sb.toString());
if (i < blocks.size() - 1) {
writer.write(System.lineSeparator());
}
sb.setLength(0); // освобождаем память
}
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
public static String readFile(String nameOfFile) {
try (
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(nameOfFile),
StandardCharsets.UTF_8
)
)
) {
StringBuilder text = new StringBuilder();
int read = reader.read();
while (read != -1) {
text.append((char) read);
read = reader.read();
}
return text.toString();
} catch (FileNotFoundException e) {
System.out.println("Input file not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException file not found: " + e.getMessage());
}
return null;
}
public static void writeToFile(String nameOfFile, String text) {
try (
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(nameOfFile),
StandardCharsets.UTF_8
)
)
) {
writer.write(text);
} catch (FileNotFoundException e) {
System.out.println("Output file not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException file not found: " + e.getMessage());
}
}
}