I am creating a program which creates reads a file into an array separating which file into a different index value in the array.
static String[] readFile () {
int count = 0;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
// reads file into array
while (scanner.hasNextLine()) {
String[] data = new String[count];
int len = data.length;
for (int i = 0; i <= len; i++) {
data[i] = scanner.nextLine();
}
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}
The problem is that when trying to return the variable data I get an error saying 'cannot resolve symbol data" because it is initialized in a try-catch block. I have tried doing this but it returns the value null because the variable's length is determined by the variable count whose's value is also determined in a catch block. Thanks in advance!
You can use #Sweeper advice from comments. It will be looks like this.
static ArrayList<String> readFile () {
ArrayList<String> data = new ArrayList<>();
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
while (scanner.hasNextLine()) {
data.add(scanner.nextLine()) ;
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
But if you want to stay with your current code, you can initialize data by null out of try block. And also you need to reset Scanner. Your code will be looking something like this. Note, that in the for loop you must use condition <len not <=len.
static String[] readFile () {
String[] data = null;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
int count = 0;
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close(); // don't forget about closing resources
data = new String[count];
// reads file into array
scanner = new Scanner(file);
for (int i = 0; i < len; i++) {
data[i] = scanner.nextLine();
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
Here are some similar questions with answers:
Java: Reading a file into an array
Read text file into an array
Also, I want to point at the try-with-resources statement - the Scanner object should be closed or initialized inside it.
Additionally, System.exit(0); is not a good way to stop a method, because all finally blocks around it wouldn't be executed in this case.
you are having 2 problems the firsthappens because the variable data is declared in the try-catch block ¿what if an instruction throws an exeption and the variable data is never declared? in this case ¿what is going to be returned?, the solution is to declare the variable data before the try-catch block, the second happens because when you invoke nextLine() the Scanner object mantains its state so when you try to invoke nextLine() again after go through the whole file it is in the last line (there is not next line), you can solve it invoking close() and then initialize the scanner object again this will reset the state:
static String[] readFile () {
Scanner scanner = null;
int count = 0;
String[] data = null;
try {
File file = new File("C:\\Users\\Mulé\\Desktop\\doc.txt"); // create file
scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close();
scanner = new Scanner(file);
// reads file into array
data = new String[count];
for (int i = 0; i < data.length; i++) {
data[i] = scanner.nextLine();
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}
Related
I made a program that can display and edit a record. The problem is that I cannot delete the file that I wanted to delete to replace it with the edited ones.
public class Laboratory {
public static void main(String[] args) throws FileNotFoundException,InterruptedException,IOException {
// paste your script here ;)
String fileName = "record.txt";
String filepath = "D:\\Programming\\Java\\Program - Script Test\\files\\" + fileName;
String in = "";
File file = new File(filepath);
Scanner fscan = new Scanner(file);
Scanner scan = new Scanner(System.in);
PrintWriter pw = null;
int linecount = 1;
String content = "";
// reads the file according to the given line count.
for(int i = 0; i < linecount; i++) {
content = fscan.nextLine();
}
// creates the template file.
String tempPath = "D:\\Programming\\Java\\Program - Script Test\\files\\" + "temp.txt";
String contentParts[] = content.split("\\|");
System.out.println(content);
System.out.println(contentParts[1]);
System.out.println(contentParts[2]);
System.out.print("change the name >> ");
in = scan.nextLine();
// edits the scanned content from the file.
String finalContent = "|" + in + "|" + contentParts[2];
System.out.println(finalContent);
file = new File(filepath);
fscan = new Scanner(file);
// scans the original record and pastes it in a new template file.
try {
pw = new PrintWriter(new FileOutputStream(tempPath));
if(linecount == 1) {
content = fscan.nextLine();
pw.println(finalContent);
while(fscan.hasNextLine()) {
content = fscan.nextLine();
pw.println(content);
}
}
else if (linecount > 1) {
for (int i = 0; i < linecount - 1; i++) {
content = fscan.nextLine();
pw.println(content);
}
pw.println(finalContent);
content = fscan.nextLine();
while (fscan.hasNextLine()) {
content = fscan.nextLine();
pw.println(content);
}
}
}
catch(FileNotFoundException e) {
System.out.println(e);
}
finally {
pw.close();
fscan.close();
}
// deletes the original record
file.delete();
} // end of method
} // script test class end
Although, I made a test program that successfully deletes a file.
public class delete {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
File file = new File("helloworld.txt");
String in;
System.out.println("Press ENTER to DELETE file.");
in = scan.nextLine();
file.delete();
} // main method end
} // program end
My file path is right and I don't really know what causes the problem. Is there a solution to fix this?
Explanation
file.delete() does not throw an error if it failed. And it failed here, as indicated by its return value being false.
Execute Files.delete(file.toPath()) instead and you will see the exact error reason, which is:
Exception in thread "main" java.nio.file.FileSystemException: D:\Programming\Java\Program - Script Test\files\record.txt: The process cannot access the file because it is being used by another process
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:274)
at java.base/sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:105)
at java.base/java.nio.file.Files.delete(Files.java:1146)
at Laboratory.main(Laboratory.java:123)
So
The process cannot access the file because it is being used by another process
Because you still have a scanner to the file open, you are blocking yourself from deleting it. Close the scanner and it will work.
Your code opens two (not one) scanner to file, one at the beginning:
Scanner fscan = new Scanner(file);
which you use during your initial loop:
for (int i = 0; i < linecount; i++) {
content = fscan.nextLine();
}
and then later on you create a second one:
fscan = new Scanner(file);
which you also close during your finally block:
fscan.close();
But you did never close the first scanner.
Solution
Add
fscan.close();
After the initial loop:
for(int i = 0; i < linecount; i++) {
content = fscan.nextLine();
}
fscan.close();
and the file.delete() will succeed.
NIO
As explained, file.delete() is a poorly designed method. Prefer Files.delete(path).
In general, if you do not have a good reason to use the old cumbersome file IO library, dont. Use NIO instead (Java 7+).
I'm having issues with using try-catch blocks in java. I'm writing a method that reads a user input file and prints it out to the console. This is what I have -
static Scanner input = new Scanner(System.in);
public static String readingFiles(String fileout) {
boolean find = false;
while(!find) {
try {
File f = new File(input.nextLine());
Scanner scan = new Scanner(f);
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
}
}
ArrayList<String> list = new ArrayList<String>();
while (input.hasNext())
{
list.add(input.nextLine());
}
String output = list.toString();
return output;
}
It just seems like a mess and I have no idea what to do with it at this point. I had it working a few times, in that it would output what the file said but then if I purposefully entered the wrong file name it would loop "file not found" endlessly and I couldn't figure out how to return the loop to the beginning so the user could input a different file name.
Now it just does nothing even when i enter the correct file name, it returns nothing until i press enter again and it'll return file not found.
I call it using this in my main menu method -
case 1:
System.out.println("You chose Read File. Enter your file name: ");
System.out.println(Question4.readingFiles(input.nextLine()));
pressEnter();
break;
edit: I now have this, which works but only prints the first line of my file?
public static String readingFiles(String fileout) {
boolean find = false;
String result = "";
while (!find) {
try {
File read = new File(fileout);
Scanner check = new Scanner(read);
result = check.nextLine();
find = true;
check.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found. Please try again.");
break;
}
}
return result;
}
Check the following code.
public static void readFiles() throws Exception {
int i = 1;
BufferedReader reader = null;
Scanner input = null;
boolean fileFound = true;
while(i <= 5){
System.out.print("Enter a file name::::");
input = new Scanner(System.in);
if(input.hasNextLine()){
try {
File f = new File(input.nextLine());
reader = new BufferedReader(new FileReader(f));
String str = null;
while((str = reader.readLine()) != null){
System.out.println(str);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
fileFound = false;
i++;
continue;
} catch (IOException e) {
System.out.println("IOException");
i++;
continue;
} catch (Exception e) {
System.out.println("Some Other Exception");
i++;
continue;
} finally{
if(fileFound)
reader.close();
}
}
i++;
}
}
Please note this method will read files 5 times. If you want to change it, you can pass an int parameter to the method and accordingly change the first while condition. Ensure you give complete path of the file with escape characters. For example, if file location is 'C:\abc.txt', you need to input 'C:\\abc.txt'. Else, it will display 'File Not Found' in console.
public class readingFiles {
public static String readingFiles(String fileout) {
try {
//find a file with the same name as the value of "fileout"
File f = new File(fileout);
Scanner scan = new Scanner(f);
//create a list to hold the file output
ArrayList<String> list = new ArrayList<String>();
//loop through the output line by line and add to the list
while (scan.hasNext())
{
list.add(scan.nextLine());
}
//convert the list into a String value to pass back to the caller
String output = list.toString();
scan.close();
return output;
} catch (FileNotFoundException e) {
//if file is not found, return a value of -1
System.out.println("File Not Found.");
return("-1");
}
}
Okay a few things:
Your first while loop is unnecessary. I think you are trying to loop through files in the folder to look for a specific file name. However the Scanner scan = new Scanner(f); line already does this.
The reason your code infinitely prints "File not found." is because you never set the find condition to true to exit the loop.
You never use the fileout value you pass into the method. And your code asks the user for the filename input twice (once in the main method, once in the readingFiles method).
Using a list, then converting to String results in an output of [line1, line2, line3, etc] not sure if this is what you want.
As for why your second attempt prints only the first line, You have removed the while loop which loops through the file reading every line, therefore it only reads one line before stopping.
I have the following code which tries to determine the dimensions of a file, but each time it executes through there is this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
I'm unaware why this is occurring. Can anyone help debug the issue? And explain why it is happening?
public void loadDistances(String fname) throws Exception {
String file = fname;
File f = new File(file);
Scanner in = null;
try {
in = new Scanner(f);
}
catch (FileNotFoundException ex) {
System.out.println("Can't find file " + file);
System.exit(1);
}
int rows = 0;
int cols = 0;
int elements = 0;
while(in.hasNext()){
while(in.next() != null){
System.out.println(elements++);
}
in.nextLine();
rows++;
}
in.close();
cols = (elements + 1) / rows;
// for debug purposes
System.out.println(rows);
System.out.println(cols);
}
Which reads in this file
0 2 3.0
1 0 2.0
2 1 7.0
2 3 1.0
3 0 6.0
// Checking for suggested answer
int tokens = 0;
String line;
Scanner tokenScanner;
Scanner fileScanner;
Scanner lineScanner;
while(fileScanner.hasNextLine()){
line = fileScanner.nextLine();
lineScanner.nextLine() = line;
while(lineScanner.hasNext()){
tokens++;
}
rows++;
}
You assign no data to your variables at all in your scanning loop, and not only that, but you read from the Scanner twice while checking it for data only once, a dangerous thing to do.
while(in.hasNext()){ // **** checking once ****
while(in.next() != null){ // **** read in and waste a token here!
System.out.println(elements++);
}
in.nextLine(); // **** read in and waste a line here
rows++;
}
in.close();
I would:
Use two Scanner variables, one, fileScanner, to read in each line of text in the file,...
And one called lineScanner to read in each token on the line.
I'd use an outer while loop, that checks fileScanner.hasNextLine(), and then calls nextLine() to read the line into a String, say called line.
I'd then create a new Scanner with the line of String created, and assign it into a lineScanner variable.
I'd use an inner while loop that loops while lineScanner.hasNext(), and reads in the data into your your variables.
I'd close the inner lineScanner at the end of the outer while loop so as not to waste resources.
Alternatively, you could use String#split(...) to split the tokens in the line read in, and then parse the Strings into numbers. For example,
public List<RowData> loadDistances(String fname)
throws FileNotFoundException, NumberFormatException {
File file = new File(fname);
Scanner fileScanner = new Scanner(file);
List<RowData> rowList = new ArrayList<RowData>();
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] tokens = line.split("\\s+");
if (tokens.length != 3) {
// throw some custom exception
}
int rowNumber = Integer.parseInt(tokens[0].trim());
int xData = Integer.parseInt(tokens[1].trim());
double yData = Double.parseDouble(tokens[2].trim());
rowList.add(new RowData(rowNumber, xData, yData));
}
if (fileScanner != null) {
fileScanner.close();
}
return rowList;
}
Edit
By using a line Scanner, I recommend creating a second Scanner, passing in the line obtained from the first Scanner, and extracting data from this second Scanner. You could use a while loop if you didn't know how many tokens to expect, but your data appears to be well defined, with each line holding an int, int, and double, and we can use this information to help us extract the proper data. You could use code something like this:
// use previous same code as above except in the while loop:
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine(); // get line
Scanner lineScanner = new Scanner(line); // create Scanner with it
int rowNumber = 0;
int xData = 0;
double yData = 0.0;
if (lineScanner.hasNextInt()) {
rowNumber = lineScanner.nextInt();
} else {
// throw a custom exception since int not found
}
if (lineScanner.hasNextInt()) {
xData = lineScanner.nextInt();
} else {
// throw a custom exception since int not found
}
if (lineScanner.hasNextDouble()) {
yData = lineScanner.nextDouble();
} else {
// throw a custom exception since double not found
}
rowList.add(new RowData(rowNumber, xData, yData));
if (lineScanner != null) {
lineScanner.close();
}
}
This is my code
public static void main(String[] args) {
File source = //
Scanner s = null;
int lineNumber =0;
ArrayList<ArrayList<Integer>> tagsArray = new ArrayList<>();
try {
s= new Scanner(source);
while (s.hasNext()) {
String[] cols = s.nextLine().split(" ");
for (int i = 0; i < cols.length; i++) {
if (cols[i].equals("1"))
tagsArray.get(i).add(lineNumber);
}
lineNumber++;
}
} catch (Exception e) {
// TODO: handle exception
}
}
When I delete the for statement it read the whole text file but when I use it it read only the first line
why?
I guess you get an Exception, but you catch and hide it instead of handling it. This is very bad! You should at least print the stacktrace of the exception.
You try to access:
tagsArray.get(i).add(lineNumber);
when tagsArray is empty. You need to instantiate each ArrayList<Integer> inside tagsArray before accessing it.
So this is what I have so far :
public String[] findStudentInfo(String studentNumber) {
Student student = new Student();
Scanner scanner = new Scanner("Student.txt");
// Find the line that contains student Id
// If not found keep on going through the file
// If it finds it stop
// Call parseStudentInfoFromLine get the number of courses
// Create an array (lines) of size of the number of courses plus one
// assign the line that the student Id was found to the first index value of the array
//assign each next line to the following index of the array up to the amount of classes - 1
// return string array
}
I know how to find if a file contains the string I am trying to find but I don't know how to retrieve the whole line that its in.
This is my first time posting so If I have done anything wrong please let me know.
You can do something like this:
File file = new File("Student.txt");
try {
Scanner scanner = new Scanner(file);
//now read the file line by line...
int lineNum = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lineNum++;
if(<some condition is met for the line>) {
System.out.println("ho hum, i found it on line " +lineNum);
}
}
} catch(FileNotFoundException e) {
//handle this
}
Using the Apache Commons IO API https://commons.apache.org/proper/commons-io/ I was able to establish this using FileUtils.readFileToString(file).contains(stringToFind)
The documentation for this function is at https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html#readFileToString(java.io.File)
Here is a java 8 method to find a string in a text file:
for (String toFindUrl : urlsToTest) {
streamService(toFindUrl);
}
private void streamService(String item) {
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.filter(lines -> lines.contains(item))
.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
When you are reading the file, have you considered reading it line by line? This would allow you to check if your line contains the file as your are reading, and you could then perform whatever logic you needed based on that?
Scanner scanner = new Scanner("Student.txt");
String currentLine;
while((currentLine = scanner.readLine()) != null)
{
if(currentLine.indexOf("Your String"))
{
//Perform logic
}
}
You could use a variable to hold the line number, or you could also have a boolean indicating if you have passed the line that contains your string:
Scanner scanner = new Scanner("Student.txt");
String currentLine;
int lineNumber = 0;
Boolean passedLine = false;
while((currentLine = scanner.readLine()) != null)
{
if(currentLine.indexOf("Your String"))
{
//Do task
passedLine = true;
}
if(passedLine)
{
//Do other task after passing the line.
}
lineNumber++;
}
This will find "Mark Sagal" in Student.txt. Assuming Student.txt contains
Student.txt
Amir Amiri
Mark Sagal
Juan Delacruz
Main.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
final String file = "Student.txt";
String line = null;
ArrayList<String> fileContents = new ArrayList<>();
try {
FileReader fReader = new FileReader(file);
BufferedReader fileBuff = new BufferedReader(fReader);
while ((line = fileBuff.readLine()) != null) {
fileContents.add(line);
}
fileBuff.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(fileContents.contains("Mark Sagal"));
}
}
I am doing something similar but in C++. What you need to do is read the lines in one at a time and parse them (go over the words one by one). I have an outter loop that goes over all the lines and inside that is another loop that goes over all the words. Once the word you need is found, just exit the loop and return a counter or whatever you want.
This is my code. It basically parses out all the words and adds them to the "index". The line that word was in is then added to a vector and used to reference the line (contains the name of the file, the entire line and the line number) from the indexed words.
ifstream txtFile;
txtFile.open(path, ifstream::in);
char line[200];
//if path is valid AND is not already in the list then add it
if(txtFile.is_open() && (find(textFilePaths.begin(), textFilePaths.end(), path) == textFilePaths.end())) //the path is valid
{
//Add the path to the list of file paths
textFilePaths.push_back(path);
int lineNumber = 1;
while(!txtFile.eof())
{
txtFile.getline(line, 200);
Line * ln = new Line(line, path, lineNumber);
lineNumber++;
myList.push_back(ln);
vector<string> words = lineParser(ln);
for(unsigned int i = 0; i < words.size(); i++)
{
index->addWord(words[i], ln);
}
}
result = true;
}
Here is the code of TextScanner
public class TextScanner {
private static void readFile(String fileName) {
try {
File file = new File("/opt/pol/data22/ds_data118/0001/0025090290/2014/12/12/0029057983.ds");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("usage: java TextScanner1"
+ "file location");
System.exit(0);
}
readFile(args[0]);
}
}
It will print text with delimeters