所以我是Java的菜鸟,我需要帮助。我正在为我的项目创建一个出勤计划。因此,如果我碰巧单击“提交”按钮,该程序会要求用户输入文件名,然后单击“确定”,它会在给定的文件路径中创建一个文本文件,问题是,我的输出是连续的行,我不知道该怎么办,我已经尝试在代码中放入许多newLine“ \ n”,但这是行不通的。
if (listen.getActionCommand().equals("Submit")) {
final234();
File dir = new File("D://blablabla");
dir.mkdir();
String name1 = JOptionPane.showInputDialog("Enter the name of the file: ");
File f = new File(dir, name1 + ".txt");
try {
if (f.createNewFile()) {
System.out.println("A new File has been created.");
op = "";
PrintWriter pw = new PrintWriter(f);
BufferedWriter bw = new BufferedWriter(pw);
for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
op += (table.getValueAt(i, j) + " ");
}
op = "\n";
}
System.out.println(op);
bw.write(op + "\n");
bw.newLine();
bw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
您可能会遇到麻烦,因为在Windows和基于Unix的系统上换行符表示的方式有所不同。在视窗它"\r\n"
,而在Unix的它确实"\n"
。确保使用正确字符的最佳方法是使用System.lineSeparator()
它将为您返回正确字符的方法。
还:
op = "\n";
意味着您只需使用“ \ n”初始化变量并覆盖其先前的内容。
op += System.lineSeparator();
每次您想向当前String添加新行时,您都可以做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句