Java 尝试将新添加的文件(图像)的名称更改为与目录中现有文件(图像)相同的名称。不起作用
我创建了一个方法,它将为我提供一个保存在旧图像的DB中的1列中的路径,名为Java 尝试将新添加的文件(图像)的名称更改为与目录中现有文件(图像)相同的名称。不起作用,java,file,Java,File,我创建了一个方法,它将为我提供一个保存在旧图像的DB中的1列中的路径,名为slika,我从中返回该字符串: String DBslika = null; try { Class.forName("com.mysql.jdbc.Driver"); con1 = java.sql.DriverManager.getConnection("jdbc:mysql://loca
slika
,我从中返回该字符串:
String DBslika = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con1 = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/studenti", "root", "");
String b=String.valueOf(id);
update = con1.prepareStatement("Select slika from studenti where ID="+b);
ResultSet rs = get.executeQuery();
while(rs.next())
{
DBslika = rs.getString(1);
}
rs.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(StudentChange.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(StudentChange.class.getName()).log(Level.SEVERE, null, ex);
}
return DBslika;
}
然后单击按钮选择并将新图像添加到原始图像辞职的目录中,我尝试添加新图像并将其名称更改为与旧图像名称相同,同时在过程中替换旧图像:
private void DodajSlikuActionPerformed(java.awt.event.ActionEvent evt) {
//Old name of the existing path of the image
String staraPutanja = ImePostojeceg();
Path stariPut = Paths.get(staraPutanja);
//Path fileStari = stariPut.getFileName();
//New name of the path of new image
String put = "C:"+File.separator+"Users"+File.separator+"boban_eyma1sy"+File.separator+"OneDrive"+File.separator+"Documents"+File.separator+"NetBeansProjects"+File.separator+"StudentInfo"+File.separator+"src"+File.separator+"Studenti"+File.separator+"";
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
skuplja= filename;
Path path = Paths.get(skuplja);
Path pathEnd=null;
try {
pathEnd = Files.move(path, path.resolveSibling(stariPut));
} catch (IOException ex) {
Logger.getLogger(StudentChange.class.getName()).log(Level.SEVERE, null, ex);
}
Path file = pathEnd.getFileName();
String fromFile = skuplja;
String toFile = put.concat(file.toString());
Path source = Paths.get(fromFile);
Path target = Paths.get(toFile);
v= "src/Studenti/" + file.toString();
try{
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
}catch(IOException e){
e.printStackTrace();
}
}
这将对旧图像进行处理,不会将其添加到新目录中,并且会以某种方式破坏它,并在它所在的桌面上以其名称给出数字82。它将有图像照片图标,并会要求寻找一个程序来打开它。我认为null正在做一些事情,但我看不到其他初始化它的方法。有什么建议吗