My program reads in text files of various sizes. It then takes the numbers from the text file and creates array lists based on the numbers. The largest file i plan on using is 286,040 KB. When I run my program and it reads the file, my program stops working.
How do I know what size is the maximum my java program can handle? Is there a way of computing how big a size a file my java program can handle?
Also, what are the best suggestions for making my program be able to hold array lists of such a large size? I have heard of hash tables, however; I have not been able to full understand the concept.
Per Request, I'm adding how I upload the file:
String name = getFileName();
Scanner scanDaily = new Scanner(new File(name));
public static String getFileName()
{ //getFileName
Scanner getName = new Scanner (System.in);
System.out.println("Please input File Name");
String fileName = getName.nextLine();
return fileName;
} //getFileName
Update : Thank you to those who responded, its been very helpful
New problem
I now want to read the numbers from the file into an arraylist
String name = getFileName();
FileReader f= new FileReader(new File(name));
BufferedReader bf=new BufferedReader(f);
Scanner sc=new Scanner(bf);
ArrayList ID = new ArrayList();
ArrayList Contract = new ArrayList();
ArrayList Date = new ArrayList();
ArrayList Open = new ArrayList();
ArrayList High = new ArrayList();
ArrayList Low = new ArrayList();
ArrayList Close = new ArrayList();
ArrayList Volume = new ArrayList();
int rows = 8;
int counter1 = 0;
//Update code to prompt user for file
ArrayList list = new ArrayList();
while (scanDaily.hasNext())
{ //while
double value = scanDaily.nextDouble();
DecimalFormat df = new DecimalFormat("#.#####");
df.format(value);
list.add(value);
} //while
before I used a scanner to read my file, and that scanner was named scandaily. Now that I have a filereader and a buffered reader, which one do i use to go through my txt file?
解决方案
When I run my program and it reads the file, my program stops
working.
I thought the problem would be this and confirmed after you have added the code. I have faced the similar problem before.
Use of Scanner directly with File object causing the problem. Because that's not buffered. Use BufferedReader instead. Using scanner with big file object directly proved to be failed. Because, that's not buffered I guess.
Scanner scanDaily = new Scanner(new File(name)); //problematic for big files.
Use BufferedReader with using FileReader instead of that. It buffers the data from file as is needed but not at once.
Example:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
import java.io.File;
...............
FileReader f=new FileReader(new File(fileName));
BufferedReader bf=new BufferedReader(f);
Scanner sc=new Scanner(bf);
So your code now becomes:
String name = getFileName();
FileReader f= new FileReader(new File(name));
BufferedReader bf=new BufferedReader(f);
Scanner sc=new Scanner(bf);
Your program hangs with your scanner code because, it is loading your big file all at once into memory and hence taking time.
Also, what are the best suggestions for making my program be able to
hold array lists of such a large size? I have heard of hash tables,
however; I have not been able to full understand the concept.
In this case, since the file size is large. I would suggest you using memory mapped file. So, that you can map the file in memory and use it access it like an array. See this link about memory mapping in java.
It seems you already know about ArrayLists.
I will brief about HashMap:
HashMap uses key value pair to store the data, you have key based on which the value is stored. You will use key to store the data and get the data.
Example:
HashMap hm=new HashMap
So this way you can use any type as key and any type as value.
HashMap hm = new HashMap
hm.set(0,"hello");
hm.set(5,"bello");
HashMap sm=new HashMap
sm.set("USA","United States of America");
sm.set("UK","United Kingdom");
sm.set("IND","India");
sm.set("AUS","Australia");
so, you can query `sm.get("AUS")` to get `"Australia"`,
I hope this will solve the problem.