How to change Java class name using Eclipse JDT API? - java

I already have this code as below, but the setName in TypeDeclaration cannot take a String as a argument, it needs a SimpleName object, while I don't know how to get a SimpleName object, maybe this idea is wrong? After all, what I need is to change the Java class name, any solutions?
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str.toCharArray()); //str is the code of .java file
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
List types = cu.types();
TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class
System.out.println("className:" + typeDec.getName());
//SimpleName sn = new SimpleName();
//sn.setIdentifier("aqaa"); //change the class name to "aqaa", but this code fails
//typeDec.setName(sn);
if we change the last 3 sentences into:
SimpleName sn = typeDec.getName();
sn.setIdentifier("aqaa");
typeDec.setName(sn);
The last setName will throw an exception:
importName: java.awt.BorderLayout start: 61 length: 29Exception in thread "main" java.lang.IllegalArgumentException
className: NI
at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1873)
at org.eclipse.jdt.core.dom.ASTNode.preReplaceChild(ASTNode.java:1935)
at org.eclipse.jdt.core.dom.AbstractTypeDeclaration.setName(AbstractTypeDeclaration.java:155)
at org.catr.JavaRefiner.parse(JavaRefiner.java:52)
at org.catr.JavaRefiner.ParseFilesInDir(JavaRefiner.java:130)
at org.catr.JavaRefiner.main(JavaRefiner.java:142)
Maybe I should post the whole class, so you guys could try the code on your own:)
package org.catr;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
public class JavaRefiner
{
static int a;
//use ASTParse to parse string
public static void parse(String str)
{
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
List imports = cu.imports();
ImportDeclaration importDec = (ImportDeclaration) imports.get(0);
System.out.println("importName: " + importDec.getName() + " start: " + importDec.getStartPosition() + " length: " + importDec.getLength());
List types = cu.types();
TypeDeclaration typeDec = (TypeDeclaration) types.get(0);
System.out.println("className: " + typeDec.getName());
SimpleName sn = typeDec.getName();
sn.setIdentifier("NII");
typeDec.setName(sn);
cu.accept(new ASTVisitor()
{
Set<String> names = new HashSet<String>();
public boolean visit( VariableDeclarationStatement state)
{
List<VariableDeclarationFragment> frags = state.fragments();
Type type = state.getType();
System.out.println("Type:\t\t\t'" + type + "'\t\t\tat line " + cu.getLineNumber(type.getStartPosition()));
for (VariableDeclarationFragment frag: frags)
{
visit2(frag);
}
return false;
}
public boolean visit2(VariableDeclarationFragment node)
{
SimpleName name = node.getName();
this.names.add(name.getIdentifier());
System.out.println("Declaration:\t\t'" + name + "'\t\t\tat line "
+ cu.getLineNumber(name.getStartPosition()));
return false; // do not continue
}
public boolean visit(SimpleName node)
{
if (this.names.contains(node.getIdentifier()))
{
System.out.println("Usage:\t\t\t'" + node + "'\t\t\tat line "
+ cu.getLineNumber(node.getStartPosition()));
}
return true;
}
});
}
//read file content into a string
public static String readFileToString(String filePath) throws IOException
{
StringBuilder fileData = new StringBuilder(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1)
{
//System.out.println(numRead);
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
}
//loop directory to get file list
public static void ParseFilesInDir() throws IOException
{
File dirs = new File(".");
String dirPath = dirs.getCanonicalPath() + File.separator + "data" + File.separator;
File root = new File(dirPath);
//System.out.println(rootDir.listFiles());
File[] files = root.listFiles ( );
String filePath = null;
for (File f : files )
{
filePath = f.getAbsolutePath();
if (f.isFile())
{
parse(readFileToString(filePath));
}
else
{
a = 0;
a = a + 1;
}
}
}
public static void main(String[] args) throws IOException
{
ParseFilesInDir();
}
}

Use following code:
List types = cu.types();
TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class
System.out.println("className:" + typeDec.getName());
SimpleName sn = typeDec.getName();
sn.setIdentifier("aqaa");
typeDec.setName(sn);

Related

I am trying to write a method that will DETECT recursion in a file but am having trouble iterating through the methods of that file

This is what my recursion detector looks like (with an error of "The method contains(String) is undefined for Method Declaration" in if (md.contains(methodName))). I am not sure how I should change this to make it work. I hope to have some advice on what I could do to iterate through each individual method and check for its methodName in it. Thank you!
RecursionDetector.java
package detectors;
import java.awt.*;
import java.util.*;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
public class RecursionDetector extends VoidVisitorAdapter <Breakpoints> {
#Override
public void visit(MethodDeclaration md, Breakpoints collector) {
String className = getClass().getName();
String methodName = md.getName().asString();
int startline = md.getRange().get().begin.line;
int endline = md.getRange().get().end.line;
final StackTraceElement[] trace = Thread.currentThread().getStackTrace();
if (md.contains(methodName)) {
}
for (int i=0; i < trace.length-1; i++) {
if( trace[i].equals(trace[trace.length-1]) ) {
super.visit(md, collector);
collector.addEmpty(className, methodName, startline, endline);
}
}
}
}
I also have a Breakpoints.java that looks like this:
package detectors;
import java.util.ArrayList;
public class Breakpoints {
private ArrayList<String> collector = new ArrayList<String>();
public Breakpoints() { }
public void addClass(String currentClass) { }
public void addMethod(String currentMethod) { }
public ArrayList<String> returnCollector() {
return new ArrayList<String>(this.collector);
}
public void addEmpty(String currentClass, String currentMethod, int startLine, int endLine) {
String n = ("className: " + currentClass + ", methodName: " + currentMethod + ", startline : " + startLine
+ ", endline : " + endLine + "\n");
if (collector.contains(n)) {
return;
}
collector.add(n);
}
}
And a Driver.java that looks like this:
package detectors;
import java.io.*;
import java.util.Scanner;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
public class Driver {
public static String data;
public static String data2 = "";
public static void main(String[] args) {
try {
File myFile = new File("/Users/weihanng/Desktop/Calculator.java");
Scanner myReader = new Scanner(myFile);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
data2 = data2.concat(data);
}
myReader.close();
CompilationUnit cu = JavaParser.parse(myFile);
Breakpoints collector = new Breakpoints();
cu.accept(new RecursionDetector(), collector);
System.out.println("Recursions: ");
System.out.println(collector.returnCollector());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

Java Path-matcher: Check the files or directories are present with wild-chars and patterns

I have a below CSV string and I want to check if given file or directory exist.
private static String dir = "/Users/swapnil.kotwal/Swapnil/myproject/build/WEB-INF/classes/test/";
private static String csvConnClasses = dir + "FirstTest*.class,"+ dir+"SecondTest.class,"+dir+"abcd/";
I tried below pice of code but I'm running it through ant getting exception java.lang.NoClassDefFoundError: org/aspectj/lang/Signature
File dir = new File(cls.substring(0, cls.lastIndexOf("/")));
String[] splits = dir.getAbsolutePath().split(dir.getPath());
String basePath = splits[0] + "build/WEB-INF/classes/" + dir.getPath();
dir = new File(basePath);
if (dir.exists() && dir.isDirectory() && dir.list().length > 0) {
final String className = getClassName(new File(cls));
File[] files = dir.listFiles(new FileFilter() {
public boolean accept(File file) {
System.out.println("File Name >>> " + file.getName());
return (file.getName().startsWith(className) && file.getName().endsWith(".class"));
}
});
if (files.length == 0) {
throw new BuildException(cls + " class not found - ");
}
if (classSet.contains(cls)) {
dups.add(cls);
}
classSet.add(cls);
} else
throw new BuildException(cls + " directory not found - ");
}
Can somebody suggest me implementation using PathMatcher/Regex to check if the given files and folders are exists.
I'm planing to use Java NIO Program to Search File Entries with GLOB Pattern.
package com.test.inspector;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.StringTokenizer;
public class SearchFile {
private static String dir = "/Users/swapnil.kotwal/Swapnil/myproject/build/WEB-INF/classes/test/";
private static String csvConnClasses = dir + "FirstTest*.class,"+ dir+"SecondTest.class,"+dir+"abcd/";
public static class SearchFileVisitor extends SimpleFileVisitor<Path> {
private final PathMatcher pathMatcher;
private int matchCount = 0;
SearchFileVisitor(String globPattern) {
pathMatcher = FileSystems.getDefault().getPathMatcher(
"glob:" + globPattern);
}
#Override
public FileVisitResult visitFile(Path filePath,
BasicFileAttributes basicFileAttrib) {
if (pathMatcher.matches(filePath.getFileName())) {
matchCount++;
System.out.println(filePath);
}
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult preVisitDirectory(Path directoryPath,
BasicFileAttributes basicFileAttrib) {
if (pathMatcher.matches(directoryPath.getFileName())) {
matchCount++;
System.out.println(directoryPath);
}
return FileVisitResult.CONTINUE;
}
public int getMatchCount() {
return matchCount;
}
}
public static void main(String[] args) throws IOException {
if (null != csvConnClasses) {
StringTokenizer st = new StringTokenizer(csvConnClasses, ",");
while (st.hasMoreTokens()) {
String cls = st.nextToken();
// Removes all whitespaces and non-visible characters like tab,
// \n etc.
cls = cls.replaceAll("\\s+", "");
Path rootPath = FileSystems.getDefault().getPath( cls.substring(0, cls.lastIndexOf("/")) );
String globPattern = (new File(cls)).getName();
SearchFileVisitor searchFileVisitor = new SearchFileVisitor(globPattern);
Files.walkFileTree(rootPath, searchFileVisitor);
System.out.println("Match Count: " + searchFileVisitor.getMatchCount());
}
}
}
}

How to obtain the cosine similarity with Lucene

Currently I try to get the cosine similarity between two document with Lucene (4.10.4).
I already read this answer about cosine similarity with Lucene , and I used this example to understand how it works with Lucene.
But when I tested with 2 same words per each document (ex: "Hello world"), I've got a cosine of similarity at 0.9999999999999998
My code look like that:
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.RealVector;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.DocsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;
public class CosineSimeTest {
static String indexName = "/tmp/CosineExample";
public static final String CONTENT = "field";
public static final int N = 2;
private final Set<String> terms = new HashSet<>();
private final RealVector v1;
private final RealVector v2;
public static void main(String[] args) {
try {
CosineSimeTest cosSim = new CosineSimeTest("hello world", "hello world");
System.out.println(cosSim.getCosineSimilarity());
} catch (IOException e) {
e.printStackTrace();
}
}
public CosineSimeTest(String s1, String s2) throws IOException {
Directory directory = createIndex(s1, s2);
IndexReader reader = DirectoryReader.open(directory);
Map<String, Double> f1 = getWieghts(reader, 0);
Map<String, Double> f2 = getWieghts(reader, 1);
reader.close();
v1 = toRealVector(f1);
System.out.println("V1: " + v1);
v2 = toRealVector(f2);
System.out.println("V2: " + v2);
}
public Directory createIndex(String s1, String s2) throws IOException {
File f = new File(indexName);
if (f.exists()) {
FileUtils.deleteDirectory(f);
}
Directory directory = FSDirectory.open(new File(indexName));
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(null, analyzer);
IndexWriter writer = new IndexWriter(directory, iwc);
addDocument(writer, s1);
addDocument(writer, s2);
writer.close();
return directory;
}
public void addDocument(IndexWriter writer, String data) throws IOException {
Document doc = new Document();
FieldType type = new FieldType();
type.setIndexed(true);
type.setStoreTermVectors(true);
type.setStoreTermVectorPositions(true);
type.freeze();
Field field = new Field(CONTENT, data, type);
doc.add(field);
writer.addDocument(doc);
}
public double getCosineSimilarity() {
double dotProduct = v1.dotProduct(v2);
System.out.println("Dot: " + dotProduct);
System.out.println("V1_norm: " + v1.getNorm() + ", V2_norm: " + v2.getNorm());
double normalization = (v1.getNorm() * v2.getNorm());
System.out.println("Norm: " + normalization);
return dotProduct / normalization;
}
public Map<String, Double> getWieghts(IndexReader reader, int docId) throws IOException {
Terms vector = reader.getTermVector(docId, CONTENT);
Map<String, Integer> docFrequencies = new HashMap<>();
Map<String, Integer> termFrequencies = new HashMap<>();
Map<String, Double> tf_Idf_Weights = new HashMap<>();
TermsEnum termsEnum = null;
DocsEnum docsEnum = null;
termsEnum = vector.iterator(termsEnum);
BytesRef text = null;
while ((text = termsEnum.next()) != null) {
String term = text.utf8ToString();
docFrequencies.put(term, reader.docFreq(new Term(CONTENT, term)));
docsEnum = termsEnum.docs(null, null);
while (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
termFrequencies.put(term, docsEnum.freq());
}
terms.add(term);
}
for (String term : docFrequencies.keySet()) {
int tf = termFrequencies.get(term);
int df = docFrequencies.get(term);
double idf = (1 + Math.log(N) - Math.log(df));
double w = tf * idf;
tf_Idf_Weights.put(term, w);
}
// System.out.println("Printing docFrequencies:");
// printMap(docFrequencies);
//
// System.out.println("Printing termFrequencies:");
// printMap(termFrequencies);
//
// System.out.println("Printing if/idf weights:");
// printMapDouble(tf_Idf_Weights);
return tf_Idf_Weights;
}
public RealVector toRealVector(Map<String, Double> map) {
RealVector vector = new ArrayRealVector(terms.size());
int i = 0;
double value = 0;
for (String term : terms) {
if (map.containsKey(term)) {
value = map.get(term);
} else {
value = 0;
}
vector.setEntry(i++, value);
}
return vector;
}
public static void printMap(Map<String, Integer> map) {
for (String key : map.keySet()) {
System.out.println("Term: " + key + ", value: " + map.get(key));
}
}
public static void printMapDouble(Map<String, Double> map) {
for (String key : map.keySet()) {
System.out.println("Term: " + key + ", value: " + map.get(key));
}
}
public void getVersionOfLucene(StandardAnalyzer analyzer) {
System.out.println("version : " + analyzer.getVersion());
}
}
What is the problem ? How to fix this ?
Thanks in advance.

IndexOutOfBoundsException in File array. What could be the issue?

So I have a directory in my local C drive.
C:/Search Files/Folder [number]/hello.txt
Inside Search Files I have four foldes named:
Folder 1
Folder 2
Folder 3
Folder 4
Inside Folder 1 I have a a file called hello.txt with some String in it.
What I want to do is grab the fileDirectory, fileName and fileContent and put it in a List of XMLMessage objects. I have pasted my main class and my XMLMessage POJO. When I run it, I am getting an indexOutOfBoundsException. I have been stuck for a couple hours now. I need another pair of eyes to look into this.
Thanks,
package org.raghav.stuff;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class GetFile {
public static void main(String[] args) throws IOException {
File[] files = new File("C:\\Search Files").listFiles();
showFiles(files);
}
public static void showFiles(File[] files) throws IOException {
String line = null;
List<XMLMessage> xmlMessageList = new ArrayList<XMLMessage>();
int i = 0;
//XMLMessage folderFile = new XMLMessage();
try {
for (File file : files) {
if (file.isDirectory()) {
String fileName = file.getName();
System.out.print(fileName);
xmlMessageList.get(i).setFileName(fileName);
//folderFile.setFileName(fileName);
showFiles(file.listFiles()); // Calls same method again.
} else {
xmlMessageList.get(i).setFileDirectory(file.getName() + file.toString());
//folderFile.setFileDirectory(file.getName() + file.toString());
System.out.print("\tFile: " + file.getName()
+ file.toString());
// System.out.println("Directory: " + file.getName());
BufferedReader in = new BufferedReader(new FileReader(file));
while ((line = in.readLine()) != null) {
xmlMessageList.get(i).setFileContent(line);
// folderFile.setFileContent(line);
System.out.print("\t Content:" + line);
}
in.close();
System.out.println();
}
i++;
}
} catch (NullPointerException e) {
e.printStackTrace();
}
System.out.println(xmlMessageList.toString());
}
}
Here is the POJO:
package org.raghav.stuff;
public class XMLMessage {
private String fileDirectory;
private String fileName;
private String fileContent;
public final String FILE_NAME = "fileName";
public final String FILE_DIRECTORY = "fileDirectory";
public XMLMessage(String fileDirectory, String fileName, String fileContent) {
this.fileDirectory = fileDirectory;
this.fileName = fileName;
this.fileContent = fileContent;
}
public XMLMessage() {
}
public String getFileDirectory() {
return fileDirectory;
}
public void setFileDirectory(String fileDirectory) {
this.fileDirectory = fileDirectory;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileContent() {
return fileContent;
}
public void setFileContent(String fileContent) {
this.fileContent = fileContent;
}
public String toString(){
String returnString = "File Directory: " + fileDirectory + "\n" + "File Name" + fileName + "\n" + "File Content: " + fileContent;
return returnString;
}
/*public String createResponseFileName(String fileName){
int lastDot = fileName.lastIndexOf('.');
String responseFileName = fileName.substring(0, lastDot) + "Response" + fileName.substring(lastDot);
return responseFileName;
}*/
/*public String createResponseFileContent(String fileContent){
this.
}*/
}
You're never populating your list. I suspect you should actually have:
for (File file : files) {
XMLMessage message = new XMLMessage();
xmlMessageList.add(message);
if (file.isDirectory()) {
String fileName = file.getName();
System.out.print(fileName);
message.setFileName(fileName);
//folderFile.setFileName(fileName);
showFiles(file.listFiles()); // Calls same method again.
} else {
... etc, using message instead of xmlMessageList.get(i)
}
}
Then you don't need the i variable at all.
I think Jon Skeet is right.
you never populate your list.
you should use your constructor
XmlMessage m = new XMLMessage( fileDirectory, fileName,fileContent)
xmlMessageList.add(m);

How to read a text file as lines are added

I have been asked to write a program that will read a file as it is updated (4 times/millisecond) and print the number of lines to the system. To do this, I have the following code:
package threadFile;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile
{
private String path;
public ReadFile(String file_name)
{
path = file_name;
}
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;
}
#SuppressWarnings("unused")
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;
}
The above code is meant to open a text file and read the number of lines there are. My issue is, getting the program to update as the file is written to (by another section of the program).The below code is a thread, and is meant to call into ReadFile for instructions.
I need the program to constantly read the contents, and accurately update the line count as it is edited.
If I understand correctly your requirement you want to use one file for Inter Process Communication (or inter thread-communication to be more exact for your case). If this is the case you probably want to use the file as MemoryMapped file.
A simple description of MemoryMapped file usage is done here.
As it was already said, Java 1.7 Watch Service is also a solution that could work.
Solution
The solution to my problem was a little more involved than expected.
Reading Files
package threadFile;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PrintReader implements Runnable
{
#SuppressWarnings("unused")
private final String taskName;
final String file_name = "C:/Users/wigginsm/Desktop/Log.txt";
public PrintReader(String name)
{
taskName = name;
}
public void run()
{
boolean loop = true;
while(loop = true)
try
{
FileReader fr = new FileReader(file_name);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
int count = 0;
while(line!=null)
{
count++;
line=br.readLine();
}
FileInputStream fileIn = new FileInputStream(file_name);
BufferedReader fileR = new BufferedReader(new InputStreamReader(fileIn));
String strLine = null, tmp;
while((tmp = fileR.readLine())!=null)
{
strLine = tmp;
}
String lastLine = strLine;
System.out.println("Last entered line: " + lastLine + "\n" + "Total number of Lines: " + count);
br.close();
fileR.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
The above class is responsible for reading the file, declared with "file_name".
Writing to Files
package threadFile;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
public class WriteToFile implements Runnable
{
#SuppressWarnings("unused")
private final String taskName;
class WriteFile
{
private String path;
private boolean append_to_file = false;
public WriteFile(String file_path, boolean append_value)
{
path = file_path;
append_to_file = append_value;
}
public void writeToFile(String timestamp) throws IOException
{
int i = 0;
while(i<1)
{
FileWriter write = new FileWriter(path, append_to_file);
Calendar current = Calendar.getInstance();
int ms = current.get(Calendar.MILLISECOND);
int minute = current.get(Calendar.MINUTE);
int second = current.get(Calendar.SECOND);
int hour = current.get(Calendar.HOUR_OF_DAY);
int day = current.get(Calendar.DAY_OF_YEAR);
int month = current.get(Calendar.MONTH)+1;
int year = current.get(Calendar.YEAR);
timestamp = day + "/" + month + "/" + year + " " + hour + ":" + minute + ":" + second + ":" + ms;
PrintWriter print_line = new PrintWriter(write);
try
{
Thread.sleep(250);
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
print_line.printf("%s" + "%n", timestamp);
print_line.close();
}
}
}
//constructor
public WriteToFile(String name)
{
taskName = name;
}
#SuppressWarnings("unused")
public synchronized void run()
{
boolean loop = true;
while(loop = true)
{
try
{
String file_name = "C:/Users/wigginsm/Desktop/Log.txt";
Calendar current = Calendar.getInstance();
int ms = current.get(Calendar.MILLISECOND);
int minute = current.get(Calendar.MINUTE);
int second = current.get(Calendar.SECOND);
int hour = current.get(Calendar.HOUR_OF_DAY);
int day = current.get(Calendar.DAY_OF_YEAR);
int month = current.get(Calendar.MONTH)+1;
int year = current.get(Calendar.YEAR);
String timestamp = day + "/" + month + "/" + year + " " + hour + ":" + minute + ":" + second + ":" + ms;
WriteFile data = new WriteFile(file_name, true);
data.writeToFile(timestamp);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
The above code is responsible for writing to the file. The only reason for the while loop to continue indefinitely is due to my program specification. This can easily be altered to fit any iteration.
Execution
package threadFile;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class execute
{
public static void main(String[] args)
{
final String file_name = "C:/Users/wigginsm/Desktop/Log.txt";
WriteToFile writes = new WriteToFile(file_name);
PrintReader reads = new PrintReader(file_name);
ExecutorService thread = Executors.newCachedThreadPool();
thread.execute(reads);
thread.execute(writes);
thread.shutdown();
}
}
This is the main class, it is responsible for handling the threading. Both PrintWrite and WriteToFile have the "synchronize" statement, as run() in both classes accesses the file.

Categories

Resources