我试图从包含表的列名的文本文件中读取,并使用java.io.PrintWriter
将getter和setter方法写入java文件。程序正在编译并成功运行,但PrintWriter不打印任何内容。因此创建了一个空白的.java
文件。
package model;
import java.io.*;
import java.util.*;
public class FileReadWrite {
public static void main(String[] args) throws FileNotFoundException, IOException{
// TODO Auto-generated method stub
try {
File file = new File("C:/Users/491003/Documents/FileReadWrite/TAB1F_Input.txt");
File file1 = new File("C:/Users/491003/Desktop/Output.java");
PrintWriter pw = new PrintWriter(file1);
Scanner sc = new Scanner(file);
String st;
String regex = "_";
pw.write("package Desktop;\n\npublic class Output{\n\n");
while (sc.hasNext()) {
st = sc.next();
if (st.contains("_")) {
st = st.replaceAll(regex,"");
st = st.toLowerCase();
pw.write("public String "+st+";\n");
pw.write("public String get"+st+"(){");
pw.write("return "+st+";}\n");
pw.write
"public void set" + st
+ "(String new" + st
+ "){" + st
+ "=new" + st
+";}\n"
);
}
//System.out.println(sc.next()+" ");
}
pw.write("\n}");
} catch(IOException e) {
e.printStackTrace();
}
}
}