file io流 ——文件的复制转移
这里讲的是将一个文件夹里的文件经过复制转移到另外一个文件夹,其实现代码如下:
int bs=0;
File allFiles =new File(“C:\Users\asus\Desktop\作业\text1”);
String []str=allFiles.list();
for(String name:str)
System.out.println(name);
for(int i=0;i<str.length;i++) {
String name=str[i];
File oldFile =new File(“C:\Users\asus\Desktop\作业\text1\”+name);
File newFile=new File(“C:\Users\asus\Desktop\作业\text2\”+name);
FileInputStream fis=new FileInputStream(oldFile);
FileOutputStream fos=new FileOutputStream(newFile);
while((bs=fis.read())!=-1) {
fos.write(bs);
}
fis.close();
fos.close();
}
项目:音乐管理系统的制作
首先是分包,分为bean 实体类,dao 数据库增删改查,service 业务层,util 工具层。
bean里存放着User.java,其内代码为:
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return “User{” +
“id=” + id +
“, username=’” + username + ‘’’ +
“, password=’” + password + ‘’’ +
‘}’;
}
dao中的Userdao里的代码如下:
public static boolean login (String username,String password)throws SQLException{
Connection connection= DBUtil.getConection();
String sql =“select * from ub_user where username=? and password=?”;
PreparedStatement preparedStatement= connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
ResultSet resultSet=preparedStatement.executeQuery();
if(resultSet.next()){
return true;
}else {
return false;
}
}
public static boolean zhuce (String usermane,String password)throws SQLException{
Connection connection =DBUtil.getConection();
String sql=“insert into ub_user (username,password)values (?,?)”;
PreparedStatement statement =connection.prepareStatement(sql);
statement.setString(1,usermane);
statement.setString(2,password);
int i =statement.executeUpdate();
if (i!=0){
return true;
} else
return false;
}
service里的MusicService代码如下:
public static void main(String[] args) throws Exception{
System.out.println("----------请输入用户名:----------");
Scanner input =new Scanner(System.in);
String username =input.next();
System.out.println("----------请输入密码:----------");
String password =input.next();
System.out.println(username+password);
Userbao.login(username,password);
boolean flag=Userbao.login(username,password);
if (flag)
{
System.out.println("----------登陆成功----------");
System.out.println("----------欢迎来到音乐管理系统----------");
}
else
{
System.out.println("----------登录失败----------");
System.out.println("----------用户注册----------");
System.out.println("请输入注册名:");
String zcm =input.next();
System.out.println("请输入注册密码:");
String mm =input.next();
System.out.println("请确认密码:");
String mm1 =input.next();
if(mm.equals(mm1))
{
Userbao.zhuce(zcm,mm);
}else
{
System.out.println("密码有误,请重新注册");
}
}
}
util的DBUtil的代码如下:
public static Connection getConection(){
Connection connection=null ;
try {
Class.forName(“com.mysql.jdbc.Driver”);
connection= DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/2?useSSL=true&characterEncoding=utf-8&user=root&password=123456”
);}catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet rs, PreparedStatement preparedStatement, Connection connection)
{
if (rs!=null)
{
try {
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException e){
e.printStackTrace();
}
}
if(connection!=null)
{
try {
connection.close();
} catch (SQLException e){
e.printStackTrace();
}
}
}