出于某种原因,虽然我已经下载了CSV文件,但我的程序无法读取它们。我的代码如下,它检查CSV文件是否存在。如果没有,则转到URL并下载并读取代码。但是,它总是重新下载代码,尽管它位于路径文件夹中。
private void loadData(String path, String url) throws IOException{
File f = new File(path);
System.out.println("looking for path " + path);
if(f.exists()) {
readSavedFile(path); //method to load data
}
else{
System.out.println("Need to download from internet");
downloadAndRead(url, path);
}
}
此代码输出
寻找路径C:\ Users \ n_000 \ workspace \ Program \ GOOG.csv 需要从互联网上下载。 寻找路径C:\ Users \ n_000 \ workspace \ Program \ CHK.csv 需要从互联网上下载。
我用来创建路径的代码是:
String save = "filename"; //in program use this is the name of the stock eg GOOG or CHK
Path currentRelativePath = Paths.get("");
String savedFolder = currentRelativePath.toAbsolutePath().toString() + "\\";
path = savedFolder+save+".csv";
答案 0 :(得分:0)
它工作正常,我没有看到任何问题,我发布了我测试的代码,希望它可能有用。
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test {
public static void main(String ar[])
{
Test test=new Test();
}
public Test()
{
String save = "GOOG"; //in program use this is the name of the stock eg GOOG or CHK
Path currentRelativePath = Paths.get("");
String savedFolder = currentRelativePath.toAbsolutePath().toString() + "\\";
String path = savedFolder+save+".csv";
String url=null;
try
{
loadData(path,url);
} catch (IOException e)
{
e.printStackTrace();
}
}
private void loadData(String path, String url) throws IOException
{
File f = new File(path);
System.out.println("looking for path " + path);
if(f.exists()) {
readSavedFile(path); //method to load data
}
else{
System.out.println("Need to download from internet");
downloadAndRead(url, path);
}
}
public void readSavedFile(String path)
{
System.out.println("Reading file");
}
public void downloadAndRead(String url,String path)
{
System.out.println("Downloding file");
}
}