import java.io.Console
import java.io.File
import java.io.IOException
import java.io.PrintWriter
import java.util.ArrayList
import java.util.List
import java.util.Scanner
public class User {
private String userName
private String userPass
public User(String userName, String userPass) {
super()
this.userName = userName
this.userPass = userPass
}
public String getUserName() {
return userName
}
public void setUserName(String userName) {
this.userName = userName
}
public String getUserPass() {
return userPass
}
public void setUserPass(String userPass) {
this.userPass = userPass
}
public static void main(String[] args) throws IOException {
String userName = null
String userPass = null
List<User> userList = new ArrayList<User>()
// Console console = System.console()
// for (int i = 0 i < 5 i++) {
// userName = console.readLine("用户名:")
// userPass = console.readLine("密码:")
// User user = new User(userName, userPass)
// userList.add(user)
// }
Scanner console = new Scanner(System.in)
for (int i = 0 i < 5 i++) {
System.out.print("用户名:")
userName = console.nextLine()
System.out.print("密码:")
userPass = console.nextLine()
User user = new User(userName, userPass)
userList.add(user)
}
console.close()
File file = new File("users.txt")
PrintWriter out = new PrintWriter(file, "UTF-8")
for (User user : userList) {
out.println(String.format("用户名: %s, 密码: %s", user.getUserName(), user.getUserPass()))
}
out.close()
}
} 用户名: 123, 密码: 333
用户名: 213, 密码: 333
用户名: asds, 密码: 123
用户名: sadas, 密码: 123
用户名: dasdas, 密码: 123
主要有用到java原生态的Io类,没有第三个包。直接上代码:
import java.io.*public class write {
public static void main(String[] args) {
write("E://123.txt", "hello")
}
public static void write(String path, String content) {
try {
File f = new File(path)
if (f.exists()) {
System.out.println("文件存在")
} else {
System.out.println("文件不存在,正在创建...")
if (f.createNewFile()) {
System.out.println("文件创建成功!")
} else {
System.out.println("文件创建失败!")
}
}
BufferedWriter output = new BufferedWriter(new FileWriter(f))
output.write(content)
output.close()
} catch (Exception e) {
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)