If I have 2 files say ABCD.txt and DEF.txt. I need to check if the String "ABCD" is present in DEF.txt and also the string "DEF" present in ABCD.txt and write the combination to a file.
Totally I have around 15000 files and each file contain nearly 50 - 3000 lines has to be searched. I wrote a piece of code, its working.. but it takes one hour to display the entire list...
Is any better way of performing this? Please suggest me.
public void findCyclicDependency(){
Hashtable<String, String> htFileNameList_1 = new Hashtable<String, String>();
Hashtable<String, String> htCyclicNameList = new Hashtable<String, String>();
FileWriter fwCyclicDepen = null;
PrintWriter outFile = null;
FileInputStream fstream = null;
FileInputStream fstream_1 = null;
DataInputStream in = null;
BufferedReader br = null;
DataInputStream in_1 = null;
BufferedReader br_1 = null;
String strSV_File_CK="";
boolean bFound = false;
File fileToSearch = null;
String strSVFileNameForComparison = "";
String strSVDependencyFileLine = "";
String strSVDFileLineExisting = "";
String strCyclicDependencyOut = "";
try {
File baseInputDirectory = new File(strInputPath);
List<File> baseInputDirListing = FileListing.getFileListing(baseInputDirectory);
// Printing out the filenames for the SodaSystem
for (File swPackage : baseInputDirListing)
{
if (swPackage.isDirectory() && swPackage.getName().endsWith("Plus")) {
List<File> currSwPackageFileListing = FileListing.getFileListing(swPackage);
System.out.println("\n swPackage File --> " + swPackage.getName() );
strCyclicDependencyOut = strOutputPath + "_"+ swPackage.getName() + "_CyclicDependency.xml";
System.out.println("\n strCyclicDependencyOut File --> " + strCyclicDependencyOut );
fwCyclicDepen = new FileWriter(strCyclicDependencyOut);
outFile = new PrintWriter(new BufferedWriter(fwCyclicDepen));
outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.write("<CyclicDependencyFile>");
for (File DependentFile : currSwPackageFileListing) {
strSV_File_CK = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();
htFileNameList_1.put(strSV_File_CK.toUpperCase(),strSV_File_CK.toUpperCase());
}
for (File DependentFile : currSwPackageFileListing)
{
fstream = new FileInputStream(DependentFile);
// Get the object of DataInputStream
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
strSVFileNameForComparison = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();
//Read File Line By Line
while ((strSVDependencyFileLine = br.readLine()) != null)
{
bFound = false;
if (strSVDependencyFileLine.toUpperCase().indexOf("INDICES") == -1)
{
//Check the current line matches any of the file name in software package folder
if (htFileNameList_1.contains(strSVDependencyFileLine.trim().toUpperCase())
&& strSVDependencyFileLine.compareTo(strSVFileNameForComparison) != 0)
{
bFound = true;
// Get the file to search
for (File searchFile : currSwPackageFileListing)
{
if((searchFile.getName().substring(0, (searchFile.getName().length() - 4)).trim()).equals(strSVDependencyFileLine))
{
fileToSearch = searchFile;
break;
}
}
// Read the file where the file name is found
fstream_1 = new FileInputStream(fileToSearch);
in_1 = new DataInputStream(fstream_1);
br_1 = new BufferedReader(new InputStreamReader(in_1));
while ((strSVDFileLineExisting = br_1.readLine()) != null)
{
if (strSVDFileLineExisting.toUpperCase().indexOf("EXTRA") == -1)
{
if (htFileNameList_1.contains(strSVDFileLineExisting.trim().toUpperCase()) && bFound
&& strSVDFileLineExisting.compareTo(strSVDependencyFileLine) != 0
&& strSVDFileLineExisting.compareTo(strSVFileNameForComparison) == 0 )
{
if(!htCyclicNameList.containsKey(strSVDependencyFileLine) &&
!htCyclicNameList.containsValue(strSVDFileLineExisting))
{
htCyclicNameList.put(strSVDFileLineExisting,strSVDependencyFileLine);
outFile.write("<CyclicDepedency FileName = \"" + strSVDFileLineExisting + "\""+ " CyclicFileName = \"" +
strSVDependencyFileLine + "\" />");
break;
}
}
}
}
}
}
else
{
bFound = false;
}
}//if current line <>
}// reach each line in the current file
outFile.write("</CyclicDependencyFile>");
}
outFile.flush();
outFile.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
Thanks
Ramm
There are several problems with your design. The most important one is repeated scanning of the file system. Try the code below.
static public void findCyclicDependency2() {
PrintWriter outFile = null;
Map<String,File> fileNames = new HashMap<String,File>();
Map<String,Set<String>> fileBackward = new HashMap<String,Set<String>>();
Map<String,Set<String>> fileForward = new HashMap<String,Set<String>>();
try {
File baseInputDirectory = new File(strInputPath);
List<File> baseInputDirListing = getFileListing(baseInputDirectory);
// Printing out the filenames for the SodaSystem
for(File swPackage:baseInputDirListing) {
if (! (swPackage.isDirectory()
|| swPackage.getName().endsWith("Plus"))) continue;
System.out.println("Loading file names");
List<File> currSwPackageFileListing = getFileListing(swPackage);
for(File dependentFile:currSwPackageFileListing) {
String name = trimName(dependentFile);
fileNames.put(name,dependentFile);
BufferedReader br = new BufferedReader(new FileReader(dependentFile));
String line;
Set<String> contFor = new HashSet<String>();
Set<String> contBack = new HashSet<String>();
while( (line=br.readLine()) != null ) {
line = line.toUpperCase().trim();
if( line.equals("EXTRA") ) continue;
if( line.equals("INDICES") ) continue;
if( line.equals(name) ) continue;
if( line.compareTo(name) == 1 ) {
contFor.add(line);
} else {
contBack.add(line);
}
}
fileBackward.put(name,contBack);
fileForward.put(name,contFor);
}
String strCyclicDependencyOut = strOutputPath + "_"
+ swPackage.getName() + "_CyclicDependency.xml";
outFile = new PrintWriter(new BufferedWriter(new FileWriter(strCyclicDependencyOut)));
outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.write("<CyclicDependencyFile>");
for(Entry<String,Set<String>> entry : fileForward.entrySet()) {
String curr = entry.getKey();
for(String other : entry.getValue()) {
Set<String> otherRefs = fileBackward.get(other);
if( otherRefs == null ) continue;
if( otherRefs.contains(curr) ) {
outFile.write("<CyclicDepedency FileName = \""
+ fileNames.get(curr).getPath()
+ "\""
+ " CyclicFileName = \""
+ fileNames.get(other).getPath()
+ "\" />");
}
}
}
outFile.write("</CyclicDependencyFile>");
outFile.flush();
outFile.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Lucene comes to my mind.
Maybe it's more efficient to index all files, then query for the file names and use the results to detect your circular dependencies.
Related
What is wrong with this code, I want to read all the files recursively on a folder to check if they contain a specific word and to check if they are write writable if they accept this condition I want to delete them.
public void del(String text) throws IOException {
if (UtilityClass.isEmpty(text)) {
throw new IOException("Teksti null");
}
int count = del(folder, text);
try (FileWriter fw = new FileWriter("C://Users//Admin//Desktop//deletewriteout")) {
fw.write(
"Ne totalin u fshine : " + count + " file-a te cilet ishin te shkrushem dhe qe permabin : " + text);
fw.flush();
}
}
private int del(File file, String text) throws IOException {
int counter = 0;
String line = null;
File[] fajllat = file.listFiles((File f) -> f.isFile() && f.canRead());
for (File f : fajllat) {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
if (line.contains(text)) {
f.delete();
counter++;
}
else if (f.isDirectory()) {
counter += del(f, text);
}
}
}
return counter;
}
You need to close the input stream before deleting the file.
private int del(File file, String text) throws IOException {
int counter = 0;
String line = null;
File[] fajllat = file.listFiles((File f) -> f.isFile() && f.canRead());
for (File f : fajllat) {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
if (line.contains(text)) {
// => Added
br.close();// We need to close read stream before delete
f.delete();
counter++;
}
else if (f.isDirectory()) {
counter += del(f, text);
}
}
}
return counter;
}
See here.
Im working on converter. I load file, start reading date from it and creating directories by year, month and day(+ another one dir) in witch ends are those converted text files. Everything is fine while creating those directories but in text files is nothing or only chunk of it.
public static void convertFile(File fileToConvert, File whereToSave, long shift) {
BufferedReader reader = null;
BufferedWriter writer = null;
String oldDate = "";
String newDate = "";
boolean boolDate = true;
try {
for (File file : fileToConvert.listFiles()) {
reader = new BufferedReader(new FileReader(file));
boolean block = true;
String line = "";
int lineCounter = 0;
while ((line = reader.readLine()) != null) {
if (lineCounter==0) {
block = true;
} else {
block = false;
}
line = line.replaceAll("[^0-9-,:+NaN.]", "");
String[] data = line.split(",");
if (block) {
data[0] = data[0].substring(0, 10) + " " + data[0].substring(10);
data[0] = SimulatorForRealData.timeShift(data[0], shift);
// ====================================================================================
newDate = data[0].substring(0, 4) + " " + data[0].substring(5, 7) + " "
+ data[0].substring(8, 10);
String savingIn = SimulatorForRealData.createDirs(whereToSave.toString(),
data[0].substring(0, 4), data[0].substring(5, 7), data[0].substring(8, 10));
File f = new File(savingIn + "\\" + FILE_NAME + ".log");
if (!newDate.equals(oldDate) && boolDate == false) {
writer.close();
boolDate = true;
} else {
oldDate = newDate;
boolDate = false;
}
writer = new BufferedWriter(new FileWriter(f));
// =====================================================================================
writer.write("<in date=\"" + data[0].substring(0, 10) + "T" + data[0].substring(11)
+ "\" t=\"1\" >\n");
writer.write(data[0] + "\n");
writer.write(0 + " " + 0 + " " + 0 + "\n");
for (int x = 0; x <= 10; x++) {
writer.write("NaN" + " ");
}
writer.write("\n");
for (String s : data) {
if (s.equals(data[0])) {
continue;
}
writer.write(s + ";");
}
writer.write("\n");
} else {
for (String s : data) {
writer.write(s + ";");
}
writer.write("\n");
}
lineCounter++;
if (lineCounter == 118) {
lineCounter = 0;
writer.write("</in>\n\n");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is method where i perform it. Can someone help me. I tried different "writers" and nothing. I have suspicious that it will be problem in closing file but i dont know for sure.
I think you should close every writer you created, not only last one.
for (File file : fileToConvert.listFiles()) {
reader = new BufferedReader(new FileReader(file));
...
writer = new BufferedWriter(new FileWriter(f));
while ((line = reader.readLine()) != null) {
....
}
writer.close();
}
writer flushes all changes on disk only when buffer is overflowed or it is closed.
I see two main problems:
You create a file every time you read a line. You should put it outside the loop (or loops, if you want only one file)
Always data is written with the same filename. It should have different filenames if you make a file for every file read.
I'm trying to modify text in a file but instead of modifying it, it is just adding a new line with the new information:
Here's my code
String id= IDSearch.getText();
String newname = NameText.getText();
String newbarcode = BarcodeText.getText();
String newsupplier= SupplierText.getText();
String newamount1= AmountText.getText();
ArrayList<Item> ItemsList = new ArrayList<>();
if (id.isEmpty() || newname.isEmpty() || newbarcode.isEmpty() || newsupplier.isEmpty() || newamount1.isEmpty()) {
JOptionPane.showMessageDialog(this, " Please Fill all fields");}
else{
try {
File Items = new File ("Items.txt");
FileReader fr = new FileReader(Items);
BufferedReader br = new BufferedReader(fr);
String data;
Item tempItem;
while ((data = br.readLine()) != null) {
tempItem = new Item(data);
if (tempItem.getID().equals(IDSearch))
{
tempItem.setItemName(newname);
tempItem.setItemBarcode(newbarcode);
tempItem.setSupplierID(newsupplier);
tempItem.setAmount(newamount1);
}
ItemsList.add(tempItem);
}
try (PrintWriter pw = new PrintWriter(new FileWriter(Items, true))) {
ItemsList.forEach((item) -> {
pw.println(newname + ";" + newbarcode+ ";" + newsupplier + ";" + newamount1);
});
JOptionPane.showMessageDialog(this, "Student Updated Succesfully");
}
}catch (IOException ex) {
}
}
}
I can't seem to be able to update the tex file the way it was supposed to update. Any help would be much appreciated!
You have append set to true. Doing new FileWriter(Items, false) or just (new FileWriter(Items) should fix the issue.
I want to write small java program to read data file first field and add seqcution number
Input file:
robert,190 vikign,...
robert,2401 windy,...
robert,1555 oakbrook,...
michell,2524 sprint,...
michell,1245 oakbrrok,...
xyz,2455 xyz drive,....
Output file should be:
robert,190 vikign,...,0
robert,2401 windy,...,1
robert,1555 oakbrook,...,2
michell,2524 sprint,...,0
michell,1245 oakbrrok,...,1
xyz,2455 xyz drive,....,0
Check first field when value change sequction number start back to 0 otherwise add sequction number by 1
here is my code:
public static void createseq(String str) {
try {
BufferedReader br = null;
BufferedWriter bfAllBWP = null;
File folderall = new File("Sort_Data_File_Out");
File[] BFFileall = folderall.listFiles();
for (File file : BFFileall) {
br = new BufferedReader(new FileReader(file));
String bwp = "FinalDataFileOut\\" + str;
bfAllBWP = new BufferedWriter(new FileWriter(bwp));
String line;
line = br.readLine();
String[] actionID = line.split("\\|");
String fullname = actionID[0].trim();
int seq = 0;
String fullnameb;
while ((line = br.readLine()) != null) {
actionID = line.split("\\|");
fullnameb = actionID[0].trim();
if(fullname.equals(fullnameb)) {
seq++;
}
else {
System.out.println(line + "======" + seq + "\n");
seq = 0;
fullname = fullnameb;
}
System.out.println("dshgfsdj "+line + "======" + seq + "\n");
}
}
}
catch(Exception letterproof) {
letterproof.printStackTrace();
}
}
The below code will fix the issue.I have updated the code if you face any pblm plz let me know :
Input :
robert,190 vikign,...
robert,2401 windy,...
robert,1555 oakbrook,...
michell,2524 sprint,...
michell,1245 oakbrrok,...
xyz,2455 xyz drive,....
Code :
public static void createseq() {
try {
File file = new File("d:\\words.txt"); //Hardcoded file for testing locally
BufferedReader br = new BufferedReader(new FileReader(file));
HashMap<String,Integer> counter = new HashMap<String, Integer>();
String line;
while((line = br.readLine())!= null)
{
String[] actionID = line.split(",");
String firstName = actionID[0];
if(counter.containsKey(firstName))
{
counter.put(firstName, counter.get(firstName) + 1);
}
else
{
counter.put(firstName,0);
}
System.out.println(line+" "+counter.get(firstName));
}
br.close();
} catch(Exception letterproof) {
letterproof.printStackTrace();
}
}
Ouput Come :
robert,190 vikign,... 0
robert,2401 windy,... 1
robert,1555 oakbrook,... 2
michell,2524 sprint,... 0
michell,1245 oakbrrok,... 1
xyz,2455 xyz drive,.... 0
public String runMsg(String fileName, File Path, int option)
{
int x = 0;
String name = " ", ret = "";
if (option == 1) {
x = fileName.indexOf(".c");
name = fileName.substring(0, x);
command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
} else if (option == 2) {
x = fileName.indexOf(".cpp");
name = fileName.substring(0, x);
command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
} else {
x = fileName.indexOf(".java");
name = fileName.substring(0, x);
command = "java " + name + " < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
}
String output = executeCommand(command, Path);
}
private String executeCommand(String command, File Path)
{
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command, null, Path);
p.waitFor();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
// BufferedReader reader3 = new BufferedReader(new InputStreamReader(p.getOutputStream()));
String line = "";
while ((line = reader1.readLine()) != null) {
output.append(line + "\n");
}
while ((line = reader2.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
I am trying to execute a .class file from another java file. The .java file will take input from a txt file located at C:\iptest\input.txt and will produce an output file at the following location C:\outputtest\out.txt but when I run this application nothing happens and the application goes into some kind of infinite loop.