Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Using multiple classes and creating an Object of a class and calling its method in a different class

In the process of creating a new class, I need to move my main method from the class SaveDate to the class DynamicTest. Below I have listed the code of both classes.The objective is to be able to run my program from the DynamicTest Class. I need help understanding the process of moving my main method to a different class and creating an Object of a class and calling its method.
public class SaveData {
private static final Map<String, Object> myCachedTreeMap = new TreeMap<String, Object>();
public static final List<String> getLines(final String resourceParam, final Charset charset) throws IOException{
System.out.println("Please get: "+resourceParam);
if (myCachedTreeMap.containsKey(resourceParam) ) {
// Use the cached file, to prevent an additional read.
System.out.println("Found in memory : "+resourceParam);
}
else {
// Load the file from disk
System.out.println("Not Found in memory : "+resourceParam);
}
return null;
}
public static void main(String[] args) throws IOException {
String target_dir = "C:\\myfiles\\config\\en";
String output = "C:\\myfiles\\config\\en\\output.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
if (files == null || files.length < 1) {
System.out.println("File list is empty...");
return;
}
// open the Printwriter
PrintWriter outputStream = new PrintWriter(output);
try {
for (File textFile : files) {
if (textFile.isFile() && textFile.getName().endsWith(".txt")) {
readFromDisk(textFile);
}
}
}
finally {
outputStream.close();
}
String fileNameFromCache = "en_synonyms.txt";
Object Sheet1 = myCachedTreeMap.get(fileNameFromCache);
System.out.println(fileNameFromCache + " : \n" + Sheet1);
}
private static void readFromDisk(File textFile) throws FileNotFoundException, IOException {
BufferedReader inputStream;
inputStream = null;
String content = "";
try {
inputStream = new BufferedReader(new FileReader(textFile));
content = readFile(textFile);
System.out.println("Bytes Read = "+content.length());
// Save contents
FileContentsObject Sheet1 = new FileContentsObject(System.currentTimeMillis(),
textFile.lastModified(), content,
textFile.getName(),
getLines(null, null));
// add to map
myCachedTreeMap.put(textFile.getName(), Sheet1);
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
private static String readFile(File f) throws FileNotFoundException, IOException, UnsupportedEncodingException {
StringBuilder text = new StringBuilder(1024);
int read, N = 1024 * 1024;
char[] buffer = new char[N];
BufferedReader br = null;
try {
br = new BufferedReader(
new InputStreamReader(
new FileInputStream(f), "UTF8"));
while(true) {
read = br.read(buffer, 0, N);
if (read > 0)
text.append(new String(buffer, 0, read));
if(read < N) {
break;
}
}
}
finally {
if (br != null)
br.close();
}
return text.toString();
}
private static final class FileContentsObject {
private long cachedTime; // currentTime
private long lastModifiedTimestamp;
private String contents;
List<String> lines;
private String fileName;
public FileContentsObject(long cachedTime, long lastModifiedTimestamp,
String contents, String fileName, List<String> lines) {
this.cachedTime = cachedTime;
this.lastModifiedTimestamp = lastModifiedTimestamp;
this.contents = contents;
this.fileName = fileName;
this.lines = lines;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
System.out.println("Current Time & Date:" + sdf.format(cachedTime));
System.out.println("Last Modified Time Stamp:"
+ sdf.format(lastModifiedTimestamp));
}
/**
*
* @return The lines from the file
*/
List<String> getLines() {
return this.lines;
}
public String toString() {
return "Sheet1{" + "fileName='" + fileName + '\'' + ", contents='"
+ contents + '\'' + ", lastModifiedTimestamp="
+ lastModifiedTimestamp + ", CurrentTime&Date="
+ cachedTime + '}';
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class DynamicTest {
public static void main(String[] args) {
Charset charset = Charset.forName("UTF-8");
try {
List<String> lines = CacheData.getLines("en_synonyms", charset) ;
if (lines != null) {
System.out.println("Number of Lines: "+lines.size());
for (String line:lines) {
System.out.println("DynamicTest:: "+line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
List<String> lines = CacheData.getLines("en_stopwords", charset) ;
if (lines != null) {
System.out.println("Number of Lines: "+lines.size());
for (String line:lines) {
System.out.println("DynamicTest:: "+line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Best Answer
-
I have read and watched videos regarding creating an Object of a class and calling its method in a different class. I'm still confused about how to properly do this. Using the code below can anyone explain how to properly call the objects method from my main.
Huh? You have NOT posted any 'main' or any 'objects method'.
If you need help with code you have to post the code.
The Java tutorials has dozens of trails on 'Classes and Objects': what they are, how to create them and how to use them.
https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
1. Create an instance of a class
2. call one or more of the public methods of that class
If the class has public static methods then you do NOT need to create an instance first.
I suggest you work your way thru those tutorials. They include WORKING example code.
Answers
-
I have read and watched videos regarding creating an Object of a class and calling its method in a different class. I'm still confused about how to properly do this. Using the code below can anyone explain how to properly call the objects method from my main.
Huh? You have NOT posted any 'main' or any 'objects method'.
If you need help with code you have to post the code.
The Java tutorials has dozens of trails on 'Classes and Objects': what they are, how to create them and how to use them.
https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
1. Create an instance of a class
2. call one or more of the public methods of that class
If the class has public static methods then you do NOT need to create an instance first.
I suggest you work your way thru those tutorials. They include WORKING example code.
-
As rp0428 suggested, please go through the tutorials first, add extra debug messages in the samples if needed, get an understanding how things works.
You have not given the full code, so we can't help much.
By looking at the content, it looks like you have written this in the Constructor. Refer the below link to understand about Constructors
https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
If the above piece of code is the constructor code, it will get executed when you create the instance of the class itself (with those number of parameters);
ex:= FileContentsObject fileContentsObject = new FileContentsObject( cachecName, lastModifiedTimeStamp,contents, fileName, lines);
I have used the same variable names as the ones defined in the class, which is not necessary. These variables has to be defined first.
Hope it helps.
Cheers
AJ
-
Thanks for the advice so far this info has proved to be extremely helpful.