Bom dia pessoal, estou fazendo um curso pela a UDEMY e estou com dificuldade para fazer o exercício proposto pelo professor que consiste em pegar arquivos .csv e instanciar um produto em uma lista.
código
package application; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Scanner; import model.entities.Product; public class Program { public static void main(String[] args) { Locale.setDefault(Locale.US); List<Product> list = new ArrayList(); Scanner sc = new Scanner(System.in); System.out.print("Enter folder path: "); String strPath = sc.nextLine(); File file = new File(strPath); String sourceFileStr = file.getParent(); try(BufferedReader br = new BufferedReader(new FileReader(sourceFileStr))) { String productFile = br.readLine(); while(productFile != null) { String[] productInfo = productFile.split(","); String name = productInfo[0]; double price = Double.parseDouble(productInfo[1]); int quantity = Integer.parseInt(productInfo[2]); list.add(new Product(name, price, quantity)); } } catch (IOException e) { System.out.println("Error:" + e.getMessage()); } } }
Erro :
run:
Enter folder path: C:\Users\User\Desktop\shop
Error:C:\Users\User\Desktop (Acesso negado)
BUILD SUCCESSFUL (total time: 1 second)
Quando tento ler o arquivo .csv ele lê normalmente através do código:
package test; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TEST { public static void main(String[] args) throws FileNotFoundException { String path = "C:\\Users\\User\\Desktop\\shop\\iphonex.csv"; try (BufferedReader br = new BufferedReader(new FileReader(path))) { String line = br.readLine(); while (line != null) { System.out.println(line); line = br.readLine(); } } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } }