I am trying to make a log reader , which will read the log ..and the user will input a search string .. i want to get the output in a excel file containing the whole line in which this string is found

try {
			FileReader fis = null;
			FileWriter fw;
			BufferedReader in = new BufferedReader(
					new FileReader(
							"C:\\Users\\Arpit\\Downloads\\CodingCompetitionDocuments\\Sample.log"));

			fw = new FileWriter("C:\\Users\\Arpit\\Documents\\demo3.xls");
			fis = new FileReader(
					"C:\\Users\\Arpit\\Downloads\\CodingCompetitionDocuments\\Sample.log");

			char[] buf = new char[128];
			/*
			 * while(true) { int n = fis.read(buf); if(n < 0) break; // end of
			 * file reached fw.write(buf, 0, n); }
			 */
			String line = "";
			ArrayList arr = new ArrayList();
			int linenum = 1;
			while ((line = in.readLine()) != null) {
				fw.write(linenum + "\t" + line);
				arr.add(line);
				fw.write("\n");
				linenum++;
			}
			String findString = "doNotRefer";
			int SizeString = findString.length();
			int first = 0, two = 0;
			String substr = "";

			for (int i = 0; i < arr.size(); i++) {
				String Line = arr.get(i).toString();

				for (int j = 0; j < Line.length(); j++) {
					if ((j + SizeString) > (Line.length())) {
						break;
					}
					substr = Line.substring(j, j + SizeString);
					// System.out.println(substr);
					if ((substr.equals("doNotRefer")) == true) {
						System.out.println(substr);
						System.out.println("Find in Line" + (i + 1));
					}

				}
			}

			// System.setOut(new PrintStream(new BufferedOutputStream(new
			// FileOutputStream("C:\\Users\\Arpit\\Documents\\demo2.txt"))));
			fis.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

Edited 6 Years Ago by peter_budo: Please do not use CODE SNIPPED for posting question. Use FORUM THREAD.

Okay? And your question would be ... ?

my question now is that I want to make a search in the file on basis of line numbers .. that is i would give starting line number and ending line number and would get the o/p in an excel file.

Okay? So readLine with a counter. And? And you can't "write an Excel" with a standard filwwritier. Google "POI HSSF" and/or "Andy Khan jxl".

The starting line number and ending line number I would get from user in a textfield that could only contain string but how would i now convert it into a int and then make a search ?

I am able to make an excel and write in it. coz I have added one more functionality in this , that is to search a string entered by the user in the textfield and then write the lines containing that string in an excel file .
Just help me solve the 1st part.

The starting line number and ending line number I would get from user in a textfield that could only contain string but how would i now convert it into a int and then make a search ?

Take a look at the API docs for the Integer class. As far as "making a search", like I said "readLine" (that's a method BTW) with a counter and then String's "contains" method (if looking for actual text).

I am able to make an excel and write in it. coz I have added one more functionality in this , that is to search a string entered by the user in the textfield and then write the lines containing that string in an excel file .
Just help me solve the 1st part.

Just because you name the file whatever.xls does not make it an Excel. And what your code above does is write to a simple text file with an "xls" extension. That is not an Excel file, but if you want to believe otherwise, go ahead. And this talk of "additional functionality" does not support your argument that it is.

Edited 6 Years Ago by masijade: n/a

Take a look at the API docs for the Integer class. As far as "making a search", like I said "readLine" (that's a method BTW) with a counter and then String's "contains" method (if looking for actual text).

"contains" will return a boolean value and I know the "readLine" method will be used to read the file, but I needed a hint for the searching by entering the start and end line number and then writing the text in a file.

Just because you name the file whatever.xls does not make it an Excel. And what your code above does is write to a simple text file with an "xls" extension. That is not an Excel file, but if you want to believe otherwise, go ahead. And this talk of "additional functionality" does not support your argument that it is.

It does make the excel file , coz I have done it. And I have added the functionality , didn't got the time to update the code here.

Well, if you know enough to be able to make a real xls file, then you should know enough to be able to put together the info about readLine, a counter, and contains to be able to do a search inside a file constraing that search between a specific set of line numbers (and help writing to the excel is also not need since you already know how to do so). But, once again, I can almost guarantee that you're still simply writing a text file (or maybe a csv as one step further that can, at least, actually be opened by excel and look like a spreadsheet, although it still isn't an excel file). Give it a try, and post your code, again, and we will help you to correct it.

Edited 6 Years Ago by masijade: n/a

The actual log file does not contain any line numbers , so what should be the delimiter I will have to use upon which I have to upgrade the counter ?

That's what I asked , how would the code know the end of line... there should be some delimiter right ? some thing like when it reads a space the counter should increment or something like that ?

I am sorry to say but this community is of no use ..no body gives straight forward answers ..rather all they do is taunt !!!!!

Comments
So go find someone else to do your (home)work for you.

I am sorry to say but this community is of no use ..no body gives straight forward answers ..rather all they do is taunt !!!!!

Your real problem is that no one gave you code. I gave you plenty of tips and hints (it is, after all, your (home)work), all you had to do was try. Take one attempt, and post that code, and we will be more than happy to give you further tips, but you have to implement those tips. No one "taunted" you. I simply pointed out that your claim of having produced an excel file was ludricous (with the code provided) and you provided no evidence to the contrary. Instead you continued to feign complete ignorance of the portion of the code you kept wanting provided to you. A piece of code, BTW, that is infinately easier than creating a "real" Excel file, and so should have provided no difficulty to you with the hints provided, given your claim of having successfully created an Excel file.

This article has been dead for over six months. Start a new discussion instead.