I cannot find the error - java

I am following the tutorial here and I have been trying to get this working for two days now. I get this error message when compiling FileData.
FileData.java:13: error: cannot find symbol
ReadFile file = new ReadFile(file_name);
^
symbol: class ReadFile
location: class FileData
FileData.java:13: error: cannot find symbol
ReadFile file = new ReadFile(file_name);
^
symbol: class ReadFile
location: class FileData
Any assistance would be greatly appreciated.
The code follows:
package textfiles;
import java.io.IOException;
public class FileData
{
public static void main(String[] args) throws IOException
{
String file_name = "C:/test.txt";
try
{
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
int i;
for (i=0; i < aryLines.length; i++)
{
System.out.println(aryLines[i]);
}
}
catch (IOException e)
{
System.out.println( e.getMessage() );
}
}
}
package textfiles;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile
{
private String path;
public ReadFile (String file_path) //ReadFile Method
{
path = file_path;
}
public String[] OpenFile() throws IOException //OpenFile method
{
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLine();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i < numberOfLines; i++)
{
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
int readLine() throws IOException //readLines Method
{
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLines;
int numberOfLines = 0;
while ((aLines = bf.readLine()) !=null)
{
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}

The problem is JAVA COMPILER is looking for CLASS FILE {ReadFile.class} inside the folder textfiles so follow this to get compile,
1--> compile the ReadFile using this
javac -d . ReadFile.java
2--> compile the FileData using this
javac -d . FileData.java
NOTE: The purpose of this
-d
is it will create appropriate packages during compilation.

Related

how to use relative file path to read a file in java?

file path is not working, input1.txt is located in the same directory as library.java.
What should i do to correct it ?
How should i give path so that it read thr text file ?
package SimpleLibrarySystem;
import java.time.LocalDateTime;
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Library
{
ArrayList <Book> var = new ArrayList<Book>();
HashMap<Book, LocalDateTime> var1 = new HashMap<Book, LocalDateTime>();
public Library(String person, LocalDateTime time)
{
try{
File myfile = new File("input1.txt") ;
Scanner br = new Scanner(myfile);
String line = br.nextLine();
while ((line != null))
{
String a = line;
line = br.nextLine();
String b = line;
Book a1 = new Book(a,b,person);
Book a2 = new Book (a,b, "");
var.add(a2);
var1.put(a1,time);
//System.out.println(a + " "+ b);
line = br.nextLine();
}
br.close();
}
catch (Exception e)
{
System.out.println("not working");
}
}
}
with code:
public class Main{
public static void main(String[] args) {
int counter = 0;
try (FileReader fileReader = new FileReader("src/file.txt"); Scanner sc = new Scanner(fileReader)) {
while (sc.hasNextLine()) {
++counter;
sc.nextLine();
}
} catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
System.out.println(counter);
}
}
checkout: how to read file in Java

Buffered reader not accessable by other methods

Sorry if this is obvious, I am inexperienced with Java. I have 2 methods, one that creates a BufferedReader, and one that processes it. However, the processing method can not access the BufferedReader, even though it is in a public method. Am I doing something wrong?
package textfiles;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile {
private String path;
public ReadFile(String filePath) {
path = filePath;
}
public void Open() throws IOException {
FileReader read = new FileReader(path);
BufferedReader buff = new BufferedReader(read);
}
public String[] OpenFile() throws IOException {
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i < numberOfLines; i++) {
textData[i] = buff.readLine();
}
buff.close();
return textData;
}
int readLines() throws IOException {
FileReader linedFile = new FileReader(path);
BufferedReader findLines = new BufferedReader(linedFile);
String lines;
int noLines = 0;
while ((lines = findLines.readLine()) != null) {
noLines++;
}
findLines.close();
return noLines;
}
}
Define BufferedReader at instance level just after declaring your path variable like
BufferedReader buff;
And in your method open, initialize it like
buff = new BufferedReader(read);
Your code should return compile time error as buff undefined variable. So declare it as instance variable and use it in any method directly.

Java I/O writing, mixingcase and getExtension problems

I am changing names of all files in directory and if it's text file I am changing the content but it doesn't seem to work the name of the file is changed right but content is blank/gone heres is my code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.io.FilenameUtils;
public class FileOps {
public static File folder = new File(
"C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files");
public static File[] listOfFiles = folder.listFiles();
public static void main(String[] argv) throws IOException {
toUpperCase();
}
public static void toUpperCase() throws FileNotFoundException {
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
String newname = mixCase(listOfFiles[i].getName());
if (listOfFiles[i].renameTo(new File(folder, newname))) {
String extension = FilenameUtils
.getExtension(listOfFiles[i].getName());
if (extension.equals("txt") || extension.equals("pdf")
|| extension.equals("docx")
|| extension.equals("log")) {
rewrite(listOfFiles[i]);
System.out.println("Done");
}
}
} else {
System.out.println("Nope");
}
}
}
public static String mixCase(String in) {
StringBuilder sb = new StringBuilder();
if (in != null) {
char[] arr = in.toCharArray();
if (arr.length > 0) {
char f = arr[0];
boolean first = Character.isUpperCase(f);
for (int i = 0; i < arr.length; i++) {
sb.append((first) ? Character.toLowerCase(arr[i])
: Character.toUpperCase(arr[i]));
first = !first;
}
}
}
return sb.toString();
}
public static void rewrite(File file) throws FileNotFoundException {
FileReader reader = new FileReader(file.getAbsolutePath());
BufferedReader inFile = new BufferedReader(reader);
try {
FileWriter fwriter = new FileWriter(file.getAbsolutePath());
BufferedWriter outw = new BufferedWriter(fwriter);
while (inFile.readLine() != null) {
String line = mixCase(inFile.readLine());
outw.write(line);
}
inFile.close();
outw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
There is several issue with your code:
Your rewrite function is perform on old name File. It should be done on the renamed File:
String newname = mixCase(listOfFiles[i].getName());
File renamedFile = new File(folder, newname);
if (listOfFiles[i].renameTo(renamedFile )) {
String extension = FilenameUtils
.getExtension(listOfFiles[i].getName());
if (extension.equals("txt") || extension.equals("pdf")
|| extension.equals("docx")
|| extension.equals("log")) {
rewrite(renamedFile);
System.out.println("Done");
}
}
you are trying to read docx and pdf file like regular text file. This cannot work. You will have to use external library like POI and pdf Box
Your rewrite function is not safe. You must unsure to close the ressources:
FileReader reader = null;
BufferedReader inFile = null;
try {
reader = new FileReader(file.getAbsolutePath());
inFile = new BufferedReader(reader);
FileWriter fwriter = new FileWriter(file.getAbsolutePath());
BufferedWriter outw = new BufferedWriter(fwriter);
while (inFile.readLine() != null) {
String line = mixCase(inFile.readLine());
outw.write(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally
{
if(infile != null)
inFile.close();
if(reader != null)
reader .close();
}
You can't re-write a file on top of itself. you need to write the new content to a new file, then rename again.

Error: Could not find or load main class ReadFile recieved when attempting to run code [duplicate]

This question already has answers here:
Running a Java Program
(5 answers)
Closed 8 years ago.
When I run some code I have compiled (with no errors) I receive the following error:
Error: Could not find or load main class ReadFile
The code I am trying to run is the file ReadFile.java:
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile {
public static void main(String[] args) throws IOException {
String file_name = "hello.txt";
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
int i;
for (i=0; i<aryLines.length; i++) {
System.out.println(aryLines[i]);
}
}
catch (IOException e) {
System.out.println(e.getMessage() );
}
}
private String path;
public ReadFile(String file_path) {
path = file_path;
}
public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0;i<numberOfLines;i++) {
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while (( aLine = bf.readLine()) != null) {
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
and hello.txt is as follows:
Hello World
Hello Solar System
Hello Galaxy
I am a beginner and am not sure why I am returned with this error, can anyone help?
First of all, change directories to the location of your source file..
Use the Java Compiler to compile the source file to a .class file
javac ReadFile.java
Now use Java to run the resulting .class file...
java ReadFile
(Note, I didn't have a file to read, but that wasn't my goal)
**** WARNING ****
This will only work if the ReadFile.java file does not start with a package declaration. If it does, it will change the way that the class has to be run!!

ReadFile cannot be resolved to a type

I am consider a rookie and I have searched for hours on the internet to solve my problem but still no luck.
I really want to understand java and if you could explain some detail that will be highly grateful.
The problem is in this line
ReadFile file = ReadFile(file_name);
error message : "ReadFile cannot be resolved to a type."
Here is my code: FileData.java
package textfiles;
import java.io.IOException;
public class FileData {
public static void main (String[] args) throws IOException {
String file_name = "D:/java/readfile/test.txt";
try {
ReadFile file = ReadFile(file_name);
String[] aryLines = file.OpenFile();
int i;
for (i =0; i < aryLines.length ; i++) {
System.out.println( aryLines[i] );
}
}
catch (IOException e) {
System.out.println( e.getMessage() );
}
}
}
And this is my other code: ReadFile.java
package textfiles;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile {
private String path;
public ReadFile (String file_path) {
path = file_path;
}
int readLines () throws IOException{
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while (( aLine = bf.readLine() ) != null) {
numberOfLines++;
}
bf.close();
return numberOfLines;
}
public String[] OpenFile () throws IOException {
FileReader fr = new FileReader (path);
BufferedReader textReader = new BufferedReader (fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i < numberOfLines; i++) {
textData[i] =textReader.readLine();
}
textReader.close();
return textData;
}
}
Try this:
ReadFile file = new ReadFile(file_name);
In order to initialize an object with it's class name you should use the new key word like this:
ClassName objName = new ClassName(arguments);
From the rest of your code seems you know the notion, nevertheless I refer you (or possible future visitors) to this page.

Categories