Java 让文件编写器将与控制台相同的内容写入html文件?
我将分三个部分来回答这个问题:第一部分将解释我的代码目前在做什么,第二部分解释我希望做什么,第三部分是我的代码 我的代码当前选择一个文本文件,然后询问用户是否希望以html或纯文本形式输出。如果用户选择html,则应将信息写入html文件,然后呈现该文件 我想做的是使它以我想要的方式显示输出,我让它为纯文本版本工作,但是当我运行html版本时,我遇到了一些问题,它不能正确地执行Java 让文件编写器将与控制台相同的内容写入html文件?,java,html,Java,Html,我将分三个部分来回答这个问题:第一部分将解释我的代码目前在做什么,第二部分解释我希望做什么,第三部分是我的代码 我的代码当前选择一个文本文件,然后询问用户是否希望以html或纯文本形式输出。如果用户选择html,则应将信息写入html文件,然后呈现该文件 我想做的是使它以我想要的方式显示输出,我让它为纯文本版本工作,但是当我运行html版本时,我遇到了一些问题,它不能正确地执行 private static void processhtml(String UserFile) throws Fil
private static void processhtml(String UserFile) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
int totgoals = 0;
int gamesplayed = 0;
int gs = 0;
int gc = 0;
int w = 0;
int d = 0;
int l = 0;
String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.
Scanner s = new Scanner(new BufferedReader(
new FileReader(UserFile))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the name of the team you want the results for");
String input = scanner.nextLine();
int count=0;
String line="";
String[] words=new String[40];
try{
String fileName="test"+count+".html";
PrintWriter writer = new PrintWriter(fileName, "UTF-8");
while (s.hasNext()) {
line =s.next();
words = line.split("\\s*:\\s*");
for(int i=0;i<words.length;i++){
writer.println(input + newLine + "--------------------------" + newLine + "Games played: " + gamesplayed + newLine + "Games Won: " + w + newLine + "Games Drawn: " + d + newLine + "Games Lost: " + l + newLine + "Goals For: " + gs + newLine + "Goals Against: " + gc);
;
}
}
writer.close();
writer.flush();
}catch(Exception e){
e.printStackTrace();
}
if(verifyFormat(words)) {
hteam = words[0]; // read the home team
ateam = words[1]; // read the away team
hscore = Integer.parseInt(words[2]); //read the home team score
totgoals = totgoals + hscore;
ascore = Integer.parseInt(words[3]); //read the away team score
totgoals = totgoals + ascore;
if ( input.equalsIgnoreCase(hteam)){
gamesplayed = gamesplayed + 1;
gs = gs + hscore;
gc = gc + ascore;
if (hscore > ascore)
w = w + 1;
else if (ascore > hscore)
l = l + 1;
else
d = d + 1;
}
else if (input.equalsIgnoreCase(ateam)){
gamesplayed = gamesplayed + 1;
gs = gs + ascore;
gc = gc + hscore;
if (hscore < ascore)
w = w + 1;
else if (ascore < hscore)
l = l + 1;
else
d = d + 1;
}
//output the data from the file in the format requested
}
}
这是我用于输出纯文本版本的代码,它确实有效。其中之一,纯文本中的换行符和html中的换行符是完全不同的。Linux换行符:
\n
Html换行符:
。我认为当试图从纯文本转换为html时,这会暴露出更大的问题。@JonnyHenly我知道html,我不确定如何格式化它,以便java文件在循环中输出为html文件String fileName=“test”+count+“.html”
当前是否没有输出到.html
?@JonnyHenly是的,但我不知道如何以我希望的方式格式化它,因为每当我尝试时,它都不会work@JonnyHenly我的意思是,当我运行它而不是得到正确的结果时,它显示出与预期完全不同的结果,这是非常重要的我很困惑。
System.out.println(input + newLine + "--------------------------" + newLine + "Games played: " + gamesplayed + newLine + "Games Won: " + w + newLine + "Games Drawn: " + d + newLine + "Games Lost: " + l + newLine + "Goals For: " + gs + newLine + "Goals Against: " + gc);