Im trying to use relative path to read a file but the path cant be found.
maxSuccession is the name of my project
This is my workspace path: C:\Programming
This is my project's path C:\Programming\MaxSuccessions\maxSuccessions
This is my filepath C:\Programming\MaxSuccessions\Tests\test1\00.in.txt
package test1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) throws IOException {
String myPath="..\\MaxSuccessions\\Tests\\test1\\";
Scanner sc = new Scanner(new File(myPath+"00.in.txt"));
System.out.println(sc.nextInt());
}
}
If your class file and required file are in the same directory, you can try this code:
public static void main(String[] args) {
String fileName = "00.in.txt"; //relative path from the path of your class.
try {
Scanner sc = new Scanner(Test.class.getResource(fileName).openStream());
System.out.println(sc.nextInt());
} catch (IOException e) {
e.printStackTrace();
}
}
Related
I have the following code that should replace an image in a folder inside Local AppData, although I get an exception saying "java.nio.file.DirectoryNotEmptyException: C:\Users\user\AppData\Local\Roblox\Versions\version-76a406688204424a\content\textures". What can I do?
Code:
package robloxstudioclassimageloader;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;
public class RobloxStudioClassImageLoader {
private static Path pathFile = Paths.get("resources/ClassImages.png");
private static Scanner scanner;
private static String path;
private static Path textureFile;
public static void main(String[] args) {
try{
scanner = new Scanner(new File("resources/path.txt"));
}catch(Exception e){
System.out.println(e);
}
path = scanner.nextLine();
textureFile = Paths.get(path);
File RobloxPath = new File(path);
if(RobloxPath.isDirectory()){
for (final File versionEntry : RobloxPath.listFiles()) { //versions folder
if (versionEntry.isDirectory()) {
for (final File contentEntry : versionEntry.listFiles()) { //version folder
if (contentEntry.isDirectory()) {
if(contentEntry.getName().equals("content")){
for (final File contentEntry2 : contentEntry.listFiles()) { //content folder
if (contentEntry2.getName().equals("textures")) {
for (final File textureEntry : contentEntry2.listFiles()) { //texture file
if(textureEntry.getName().equals("ClassImages.PNG") || textureEntry.getName().equals("ClassImages.png")) {
System.out.println(textureEntry.getName());
Path endPath = Paths.get(textureEntry.getParent());
System.out.println(endPath);
try{
Files.copy(pathFile,endPath,StandardCopyOption.REPLACE_EXISTING);
}catch(Exception e){
System.out.println(e);
}
System.out.println("ENDED");
System.exit(0);
break;
}
}
}
}
}
}
}
}
}
}
System.exit(0);
}
}
Although file(deneme.txt) is in the same src file with runner class. Why cannot I use just file name?
import java.io.File;
public class Runner {
public static void main(String []args) {
//File file = new File("deneme.txt");
File file = new File("C:\\Users\\serha\\eclipse-workspace\\HW1\\src\\deneme.txt");
System.out.println(file.exists());
System.out.println(file.isFile());
}
}
I'm trying to read a plain text file but somehow FileReader is not finding my text file. I checked the directory using getAbsolutefile() and /Users/djhanz/IdeaProjects/datalab2/pg174.txt is the exact location of the file. I tried datlab2/pg174.txt and everything I possibly could.
Here is my code
public class Program1 {
public static void main(String[] args) {
System.out.println(new File("pg174.txt").getAbsoluteFile());
Scanner testScanner = new Scanner(new BufferedReader(new FileReader("/Users/djhanz/IdeaProjects/datalab2/pg174.txt")));
while (testScanner.hasNextLine())
{
System.out.println(testScanner.nextLine());
}
}
}
The text file is under the same project directory called datalab.
Can someone please enlighten me?
Use Scanner testScanner = new Scanner(new BufferedReader(new FileReader("/pg174.txt")));
FileReader("/pg174.txt") instead of FileReader("pg174.txt").
package com.example.demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) throws FileNotFoundException {
System.out.println(new File("pg174.txt").getPath());
System.out.println(new File("pg174.txt").getAbsoluteFile());
System.out.println(new File("pg174.txt").getAbsolutePath());
Scanner testScanner = new Scanner(new BufferedReader(new FileReader("/pg174.txt")));
while (testScanner.hasNextLine()) {
System.out.println(testScanner.nextLine());
}
}
}
Output:
On the same directory of my Main.java file, I have a package/folder named database, and inside the database package I have a file named Data.txt.
This is my code of Main.java, but it is throwing this error:
java: exception java.io.FileNotFoundException
How can I get the file from a relative file? I'm used to web development, and usually something with a . dot like "./folder/file.txt" works.
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
readFile();
}
public static void readFile() {
File file = new File("./database/Data.txt");
Scanner scanner = new Scanner(file);
try {
while (scanner.hasNextLine()) {
int i = scanner.nextInt();
System.out.println(i);
}
scanner.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
You are not importing FileNotFoundException class. also, scanner statement throws the exception which should inside try. Solution is as below.
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
readFile();
}
public static void readFile() {
File file = new File("database/Data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
int i = scanner.nextInt();
System.out.println(i);
}
scanner.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Only check if those content can read using scanner or not. Content having int properly. otherwise it will throw java.util.InputMismatchException.
Are you working on a mac or windows system.
I am on windows and ".\database\Data.txt" would most probably work depending on where the file is in your file structure.
I have two Java applications. One application will contain resource files and will be used as library to other Java application.
First app com.test.resourceusing.MainClass.java which contains res/base.xml resource file.
package com.test.resourceusing;
import java.io.BufferedInputStream;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class MainClass {
public MainClass() {
super();
}
public static void main(String[] args) {
MainClass main = new MainClass();
try {
main.start();
} catch (MalformedURLException e) {
}
}
public void start() throws MalformedURLException {
URL url = getClass().getResource("res/base.xml");
System.out.println(url.getPath());
System.out.println(url.getFile());
File f = new File(url.getFile());
if (f.exists()) {
System.out.println("File exist!");
BufferedInputStream result = (BufferedInputStream)
getClass().getResourceAsStream("res/base.xml");
Scanner scn = new Scanner(result);
while(scn.hasNext()){
System.out.println(scn.next());
}
} else {
System.out.println("Not working! :(");
}
}
}
Result is:
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
File exist!
<?xml
version='1.0'
encoding='utf-8'?>
<schema>
</schema>
Then I create .jar file which contains all resource files and try to use it as library in other application.
Second app:
resourcetest.MainClassTest.java
package resourcetest;
import com.test.resourceusing.MainClass;
import java.net.MalformedURLException;
public class MainClassTest {
public MainClassTest() {
super();
}
public static void main(String[] args) {
MainClass main = new MainClass();
try {
main.start();
} catch (MalformedURLException e) {
}
}
}
Result is:
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
Not working! :(
I don't understand why it's not working, is there problems in my code? Or this solution is not possible in Java?
Do you see the difference in the location of those files?
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
You cannot access a resource that is located in a JAR file with the File API.
Your code is already on the way. A simple edit should work:
public void start() throws IOException {
URL url = getClass().getResource("res/base.xml");
if (url != null) {
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println("File exist!");
try(InputStream result = url.openStream()) {
try(Scanner scn = new Scanner(result)) {
while(scn.hasNext()) {
System.out.println(scn.next());
}
}
}
} else {
System.out.println("Not working! :(");
}
}
After deeper searching looks like I found a reason - https://stackoverflow.com/a/10605316/1117515.
In this case I need to use getResourceAsStream().