Criação de arquivo e subpasta no JAVA
galera estou fazendo uma aplicação para cria uma subpasta e dentro dessa subpasta gravar um determinado arquivo.csv o código não mostra nenhum erro, mas na hora de compilar aparece o seguinte erro :
Enter a path
D:\\\\\\\\temp
Exception in thread "main" java.io.FileNotFoundException: D:\\\\\\\\temp (Acesso negado)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at application.Program.main(Program.java:36)
Segue o código abaixo
package application;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import entites.Product;
public class Program {
public static void main(String[] args) throws ParseException, IOException, Exception {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
List <Product> list = new ArrayList<>();
System.out.println("Enter a path");
String sourceFileStr = sc.nextLine();
File sourceFile = new File(sourceFileStr);
String sourceFolderStr = sourceFile.getParent();
//Cria a pasta
boolean success = new File (sourceFolderStr+"\\\\\\\\\\\\\\\\out").mkdir();
String targetFileStr = sourceFolderStr + "\\\\\\\\\\\\\\\\out\\\\\\\\\\\\\\\\sumary.csv";
try (BufferedReader br = new BufferedReader(new FileReader(sourceFileStr))){
String itemCSV = br.readLine();
while (itemCSV != null) {
String [] fields = itemCSV.split(",");
String name = fields[0];
double price = Double.parseDouble(fields[1]);
int quantity = Integer.parseInt(fields[2]);
list.add(new Product(name, price, quantity));
itemCSV = br.readLine();
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter(targetFileStr))){
for (Product item : list ) {
bw.write(item.getName() + "," + String.format("%.2f", item.total()));
bw.newLine();
}
System.out.println(targetFileStr+"Created");
}
catch (IOException e ) {
System.out.println("Error Write File: "+ e.getMessage());
}
sc.close();
}
}
}
Código da classe entites criado abaixo
package entites;
public class Product {
private String name;
private Double price;
private Integer quantity;
public Product(String name, Double price, Integer quantity) {
super();
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public double total() {
return price * quantity;
}
}
Enter a path
D:\\\\\\\\temp
Exception in thread "main" java.io.FileNotFoundException: D:\\\\\\\\temp (Acesso negado)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at application.Program.main(Program.java:36)
Segue o código abaixo
package application;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import entites.Product;
public class Program {
public static void main(String[] args) throws ParseException, IOException, Exception {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
List <Product> list = new ArrayList<>();
System.out.println("Enter a path");
String sourceFileStr = sc.nextLine();
File sourceFile = new File(sourceFileStr);
String sourceFolderStr = sourceFile.getParent();
//Cria a pasta
boolean success = new File (sourceFolderStr+"\\\\\\\\\\\\\\\\out").mkdir();
String targetFileStr = sourceFolderStr + "\\\\\\\\\\\\\\\\out\\\\\\\\\\\\\\\\sumary.csv";
try (BufferedReader br = new BufferedReader(new FileReader(sourceFileStr))){
String itemCSV = br.readLine();
while (itemCSV != null) {
String [] fields = itemCSV.split(",");
String name = fields[0];
double price = Double.parseDouble(fields[1]);
int quantity = Integer.parseInt(fields[2]);
list.add(new Product(name, price, quantity));
itemCSV = br.readLine();
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter(targetFileStr))){
for (Product item : list ) {
bw.write(item.getName() + "," + String.format("%.2f", item.total()));
bw.newLine();
}
System.out.println(targetFileStr+"Created");
}
catch (IOException e ) {
System.out.println("Error Write File: "+ e.getMessage());
}
sc.close();
}
}
}
Código da classe entites criado abaixo
package entites;
public class Product {
private String name;
private Double price;
private Integer quantity;
public Product(String name, Double price, Integer quantity) {
super();
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public double total() {
return price * quantity;
}
}
Victhor409
Curtidas 0
Melhor post
Thiago Moreno
09/06/2019
File sourceFile = new File(sourceFileStr); /* * Aqui voce digita o diretorio que quer criar certo? * Por exemplo D:\\TEMP ? * No caso do java precisa de duas barras entao seria apenas D:\\\\TEMP" */ System.out.println("Enter a path"); String sourceFileStr = sc.nextLine(); File sourceFile = new File(sourceFileStr); /* * Se voce criou a pasta ali em cima, e usou agora getParent(), voce esta saindo da pasta * e esta voltando para a unidade D:\\. * Entao nao ha necessidade nenhuma de digitar esse getParent(), teria que ser um getAbsolutePath(); */ String sourceFolderStr = sourceFile.getParent(); //Cria a pasta /* * Aqui voce trocaria isso por: * File (sourceFolderStr + File.separator + "out").mkdir(); */ boolean success = new File (sourceFolderStr+"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\out").mkdir(); /* * Aqui o mesmo principio * File (sourceFolderStr + File.separator + "out" + File.separator + "sumary.csv"); */ String targetFileStr = sourceFolderStr + "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\out\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\sumary.csv";
GOSTEI 1
Mais Respostas
Victhor409
31/05/2019
File sourceFile = new File(sourceFileStr); /* * Aqui voce digita o diretorio que quer criar certo? * Por exemplo D:\\\\TEMP ? * No caso do java precisa de duas barras entao seria apenas D:\\\\\\\\TEMP" */ System.out.println("Enter a path"); String sourceFileStr = sc.nextLine(); File sourceFile = new File(sourceFileStr); /* * Se voce criou a pasta ali em cima, e usou agora getParent(), voce esta saindo da pasta * e esta voltando para a unidade D:\\\\. * Entao nao ha necessidade nenhuma de digitar esse getParent(), teria que ser um getAbsolutePath(); */ String sourceFolderStr = sourceFile.getParent(); //Cria a pasta /* * Aqui voce trocaria isso por: * File (sourceFolderStr + File.separator + "out").mkdir(); */ boolean success = new File (sourceFolderStr+"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\out").mkdir(); /* * Aqui o mesmo principio * File (sourceFolderStr + File.separator + "out" + File.separator + "sumary.csv"); */ String targetFileStr = sourceFolderStr + "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\out\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\sumary.csv";
GOSTEI 0
Victhor409
31/05/2019
OK obrigado pela ajuda
GOSTEI 0