gpt4 book ai didi

java - java中属性的使用

转载 作者:行者123 更新时间:2023-12-01 06:06:42 24 4
gpt4 key购买 nike

我是新来的,我想问一下properties如何与java代码一起工作,我的意思是properties内部的values code> 将用作变量。例如,我在 config.properties 中有 file1.txtfile2.txt 并将其存储在 Arraylist 中,然后扫描文件夹,如果找到文件,则将其复制。我的工作仅显示存储在数组列表中的属性中的数据名称,但我的另一个问题是如何将这些数据复制到另一个文件夹。

到目前为止,这是我的代码

     public class MainClass {

static Properties prop = new Properties();
static InputStream input = null;
static String filename = "";

public static void main(String[] args) throws IOException {

File source = new File("D:/ojt");
File dest = new File("D:/ojt/New folder");


Properties prop = new Properties();
InputStream input = null;


try {

// getFiles(filename);


} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;

try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
private static void copy(String fromPath, String outputPath)
{
// filter = new FileTypeOrFolderFilter(fileType);
File currentFolder = new File(fromPath);
File outputFolder = new File(outputPath);
scanFolder(currentFolder, outputFolder);


}

private static void getFiles(String path) throws IOException{
//Put filenames in arraylist<string>
String filename = "bydatefilesdir.props";
input = MainClass.class.getClassLoader().getResourceAsStream(filename);
Scanner s = new Scanner(input);
File dir = new File(path);
final ArrayList<String> list = new ArrayList<String>();
while (s.hasNextLine()){
list.add(s.nextLine());
}
// ArrayList<String> filenames = new ArrayList<String>();
// for(File file : dir.listFiles()){
// filenames.add(file.getName());
// }

prop.load(input);
//Check if the files are in the arraylist
for (int i = 0; i < list.size(); i++){
String s1 = list.get(i);
System.out.println("File "+i+" : "+s1);
}
System.out.println("\n");
}

private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;

try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
private static void copy(String source, String dest)
{
// filter = new FileTypeOrFolderFilter(fileType);
File currentFolder = new File(source);
File outputFolder = new File(dest);
scanFolder(currentFolder, outputFolder);


}

private static void scanFolder(File source, File dest)
{
System.out.println("Scanning folder [" + source.toString() + "]...\n");
File[] files = source.listFiles();
for (File file : files) {
if (file.isDirectory()) {
scanFolder(source, dest);
} else {
try {
copyFileUsingStream(source, dest);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

PS:对于刚刚接触 java 的糟糕编程感到抱歉......仍在学习
编辑:我已经包含了上面更新的代码..

最佳答案

我想 java.util.Properties 具有内置方法来实现您正在尝试的目标。请引用这个example你会找到更好的解决方案。

public class App {

public static void main( String[] args ){

Properties prop = new Properties();
InputStream input = null;

try {

String filename = "config.properties";
input = App.class.getClassLoader().getResourceAsStream(filename);
if(input==null){
System.out.println("Sorry, unable to find " + filename);
return;
}

//load a properties file from class path, inside static method
prop.load(input);

//get the property value and print it out
System.out.println(prop.getProperty("file1.txt"));
System.out.println(prop.getProperty("file2.txt"));


} catch (IOException ex) {
ex.printStackTrace();
} finally{
if(input!=null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

关于java - java中属性的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com