자바를 사용하여 텍스트 내용을 웹 페이지로 변환하는 실현 방법 공유
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class FileHelper {
public static String readFile(String filename) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String ans = "", line = null;
while((line = reader.readLine()) != null){
ans += line + "\r
";
}
reader.close();
return ans;
}
public static void writeFile(String content, String filename) throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(content);
writer.flush();
writer.close();
}
public static void main(String[] args) throws Exception {
String ans = readFile("D:\\input.txt");
writeFile(ans, "D:\\output.txt");
}
}
그리고 File Helper 클래스를 바탕으로 웹 페이지 메이커 클래스를 작성합니다. 그create Page 방법은 특정 파일의 내용을 특정한 웹 페이지에 생성하는 데 사용됩니다.여기서 코드를 삽입하려면 코드를 삽입할 수 있습니다.
import java.util.StringTokenizer;
public class WebpageMaker {
public static String initBegin() {
String s = "<!doctype html><html><head><title></title></head><body>\r
";
return s;
}
public static String initEnd() {
String s = "\r
</body></html>\r
";
return s;
}
public static void createPage(String inputfilename, String outputfilename) throws Exception {
String content = FileHelper.readFile(inputfilename);
StringTokenizer st = new StringTokenizer(content, "\r
");
String ans = "";
ans += initBegin();
boolean isCoding = false;
while(st.hasMoreElements()) {
String s = st.nextToken();
int len = s.length();
for(int i=0;i<len;i++) {
if(i+6 <= len && s.substring(i,i+6).equals("<alex>")) {
isCoding = true;
ans += "<pre style=\"background-color:aliceblue\">";
i += 5;
continue;
}
if(i+7 <= len && s.substring(i,i+7).equals("</alex>")) {
isCoding = false;
ans += "</pre>";
i += 6;
continue;
}
char c = s.charAt(i);
if(c == '\"') ans += """;
else if(c == '&') ans += "&";
else if(c == '<') ans += "<";
else if(c == '>') ans += ">";
else if(c == ' ') ans += " ";
else if(c == '\t') ans += " ";
else ans += c;
}
if(false == isCoding)
ans += "<br />\r
";
else
ans += "\r
";
}
ans += initEnd();
FileHelper.writeFile(ans, outputfilename);
}
public static void main(String[] args) throws Exception {
createPage("D://test.txt", "D://test.html");
}
}
예:입력 파일:test.txt
hello world!
:)
#include
int main() {
printf("hello world!
");
return 0;
}
출력 파일:test.html
<!doctype html><html><head><title></title></head><body>
hello world!<br />
:)<br />
<pre style="background-color:aliceblue">#include <stdio.h>
int main() {
printf("hello world!
");
return 0;
}</pre><br />
</body></html>
효과는 다음과 같습니다.
hello world!
:)
#include <stdio.h>
int main() {
printf("hello world!
");
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.