Convert text file to camel case then save it - java

I got the code to remove the spaces in between the words but cant get it to capitalize beginning of each word. can any find what the problem is. it needs to be in camelcase.
Orginal question is - Write a Java program that will read a text file containing unknown lines of strings, turn the whole file into camelCase, and finally save the camelCase into another text file.
package p3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CamelCase {
public static void main(String[] args) throws IOException {
String Str = null;
File file = new File("txt.txt");
if(!file.exists()) {
System.out.println("The file does not exist.");
System.exit(0);
}
Scanner filescanner = new Scanner(file);
while (filescanner.hasNext()) {
Str= filescanner.nextLine();
System.out.println(Str);
}
filescanner.close();
char[] characters = Str.toCharArray();
boolean capitalizeWord = true;
for (int i = 0; i < characters.length; i++) {
char c = characters[i];
if (Character.isWhitespace(c)) {
capitalizeWord = true;
}
else if (capitalizeWord) {
capitalizeWord = false;
characters[i] = Character.toUpperCase(c);
}
String capsandnospace = Str.replaceAll("\\s","");
FileWriter fw = new FileWriter("CamelCase.txt");
PrintWriter pw= new PrintWriter("CamelCase.txt");
pw.println(capsandnospace);
pw.close();
}

This code
while (filescanner.hasNext()) {
Str= filescanner.nextLine();
System.out.println(Str);
}
is looping through the file replacing the contents of Str with the current line.
After the loop has finished, the value of Str will be that of the last line.
You need to do your conversion of the string (and writing of the result file) in the loop

Related

How to add a column to CSV which consists of data in Java

Can we add a new column to CSV as the last column which already has let's say 3 columns with some data in it? So this new one will be added later as 4th column moreover for every row it should have random numbers.
Example,
Id Name Address Calculated
1 John U.K. 341679
2 Vj Aus 467123
3 Scott U.S. 844257
From what I understand this will require first to read csv, for loop may be to iterate to the last column and then add a new calculated column i.e Write to csv. And to add values may be the Random class of Java. But how exactly can this be done is the real question. Like a sample code would be helpful.
Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Demo1 {
public static void main(String[] args) throws IOException {
String csvFile = "C:\\MyData\\Input.csv";
String line = "";
String cvsSplitBy = ",";
String newColumn = "";
List<String> aobj = new ArrayList<String>();
/* Code to read Csv file and split */
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null)
{
String[] csvData = line.split(cvsSplitBy);
int arrayLength = csvData.length;
}
}
/* Code to generate random number */
String CHARS = "1234567890";
StringBuilder random = new StringBuilder();
Random rnd = new Random();
while (random.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * CHARS.length());
random.append(CHARS.charAt(index));
}
String finaldata = random.toString();
}
}
Great, so based on the code you provide, this could look like the following
(just to give you the idea - I write it here on the fly without testing...)
:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Demo1 {
//moved your random generator here
public static String getRandomNumber() {
/* Code to generate random number */
String CHARS = "1234567890";
StringBuilder random = new StringBuilder();
Random rnd = new Random();
while (random.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * CHARS.length());
random.append(CHARS.charAt(index));
}
String finaldata = random.toString();
return finaldata;
}
public static void main(String[] args) throws IOException {
String csvFile = "C:\\MyData\\Input.csv";
String temporaryCsvFile = "C:\\MyData\\Output_temp.csv";
String line = "";
String cvsSplitBy = ",";
String newColumn = "";
List<String> aobj = new ArrayList<String>();
/* Code to read Csv file and split */
BufferedWriter writer = new BufferedWriter(new FileWriter(
temporaryCsvFile));
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null)
{
//String[] csvData = line.split(cvsSplitBy);
//int arrayLength = csvData.length;
//actually you don't even need to split anything
String newFileLine = line + cvsSplitBy + getRandomNumber();
// ... We call newLine to insert a newline character.
writer.write(newFileLine);
writer.newLine();
}
}
writer.close();
//Now delete the old file and rename the new file
//I'll leave this to you
}
}
Based on #Plirkee sample code and his help I made a final working code. Sharing it here so that it might be useful for someone with a similar requirement.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Demo1 {
public static String getRandomNumber() {
String CHARS = "1234567890";
StringBuilder random = new StringBuilder();
Random rnd = new Random();
while (random.length() < 18) // length of the random string.
{
int index = (int) (rnd.nextFloat() * CHARS.length());
random.append(CHARS.charAt(index));
}
String finaldata = random.toString();
return finaldata;
}
public static void main(String[] args) throws IOException {
File sourceCsvFile = null;
File finalCsvFile = null;
// String sourceCsvFileName = "";
sourceCsvFile = new File("C:\\MyData\\Input.csv");
finalCsvFile = new File("C:\\MyData\\Input_1.csv");
String line = "";
String cvsSplitBy = ",";
BufferedWriter writer = new BufferedWriter(new FileWriter(finalCsvFile));
try (BufferedReader br = new BufferedReader(new FileReader(sourceCsvFile))) // read the actual Source downloaded csv file
{
line = br.readLine(); // read only first line
String newFileLine = line + cvsSplitBy + "HashValue"; // append "," and new column <HashValue>
writer.write(newFileLine); // will be written as first line in new csv
writer.newLine(); // go to next line for writing next lines
while ((line = br.readLine()) != null) // this loop to write data for all lines except headers
{
newFileLine = line + cvsSplitBy + getRandomNumber(); // will add random numbers for each row
writer.write(newFileLine);
writer.newLine();
}
}
writer.close();
if(finalCsvFile.exists() && finalCsvFile.length() > 0)
{
System.out.println("New File with HashValue column created...");
if(sourceCsvFile.delete())
{
System.out.println("Old File deleted successfully...");
}
else
{
System.out.println("Failed to delete the Old file...");
}
}
else if (!finalCsvFile.exists())
{
System.out.println("New File with HashValue column not created...");
}
}
}

Finishing File Class

I keep getting an error telling me lineNumber cannot be resolved to a variable? I'm not really sure how to fix this exactly. Am I not importing a certain file to java that helps with this?
And also how would I count the number of chars with spaces and without spaces.
Also I need a method to count unique words but I'm not really sure what unique words are.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.List;
public class LineWordChar {
public void main(String[] args) throws IOException {
// Convert our text file to string
String text = new Scanner( new File("way to your file"), "UTF-8" ).useDelimiter("\\A").next();
BufferedReader bf=new BufferedReader(new FileReader("way to your file"));
String lines="";
int linesi=0;
int words=0;
int chars=0;
String s="";
// while next lines are present in file int linesi will add 1
while ((lines=bf.readLine())!=null){
linesi++;}
// Tokenizer separate our big string "Text" to little string and count them
StringTokenizer st=new StringTokenizer(text);
while (st.hasMoreTokens()){
s = st.nextToken();
words++;
// We take every word during separation and count number of char in this words
for (int i = 0; i < s.length(); i++) {
chars++;}
}
System.out.println("Number of lines: "+linesi);
System.out.println("Number of words: "+words);
System.out.print("Number of chars: "+chars);
}
}
abstract class WordCount {
/**
* #return HashMap a map containing the Character count, Word count and
* Sentence count
* #throws FileNotFoundException
*
*/
public static void main() throws FileNotFoundException {
lineNumber=2; // as u want
File f = null;
ArrayList<Integer> list=new ArrayList<Integer>();
f = new File("file_stats.txt");
Scanner sc = new Scanner(f);
int totalLines=0;
int totalWords=0;
int totalChars=0;
int totalSentences=0;
while(sc.hasNextLine())
{
totalLines++;
if(totalLines==lineNumber){
String line = sc.nextLine();
totalChars += line.length();
totalWords += new StringTokenizer(line, " ,").countTokens(); //line.split("\\s").length;
totalSentences += line.split("\\.").length;
break;
}
sc.nextLine();
}
list.add(totalChars);
list.add(totalWords);
list.add(totalSentences);
System.out.println(lineNumber+";"+totalWords+";"+totalChars+";"+totalSentences);
}
}
In order to get your code running you have to do at least two changes:
Replace:
lineNumber=2; // as u want
with
int lineNumber=2; // as u want
Also, you need to modify your main method, you can not throw an exception in your main method declaration because there is nothing above it to catch the exception, you have to handle exceptions inside it:
public static void main(String[] args) {
// Convert our text file to string
try {
String text = new Scanner(new File("way to your file"), "UTF-8").useDelimiter("\\A").next();
BufferedReader bf = new BufferedReader(new FileReader("way to your file"));
String lines = "";
int linesi = 0;
int words = 0;
int chars = 0;
String s = "";
// while next lines are present in file int linesi will add 1
while ((lines = bf.readLine()) != null) {
linesi++;
}
// Tokenizer separate our big string "Text" to little string and count them
StringTokenizer st = new StringTokenizer(text);
while (st.hasMoreTokens()) {
s = st.nextToken();
words++;
// We take every word during separation and count number of char in this words
for (int i = 0; i < s.length(); i++) {
chars++;
}
}
System.out.println("Number of lines: " + linesi);
System.out.println("Number of words: " + words);
System.out.print("Number of chars: " + chars);
} catch (Exception e) {
e.printStackTrace();
}
}
I've used a global Exception catch, you can separate expetion in several catches, in order to handle them separatedly. It gives me an exception telling me an obvious FileNotFoundException, besides of that your code runs now.
lineNumber variable should be declared with datatype.
int lineNumber=2; // as u want
change the first line in the main method from just lineNumber to int lineNumber = 2 by setting its data type, as it is important to set data type of every variable in Java.

Error searching for word in file

In a file of randomly generated passwords my goal is to ask for a password, check the 'codes.txt' file to see if it exists, say 'LOGIN COMPLETE' for 5 seconds, delete the password, then close the files. When I reach the while loop I nothing works the way I need it to. It has all kinds of different results in various situation, none of which I can understand. I haven't even figured out how to delete the stuff on the console after 5 seconds have passed printing 'LOGIN COMPLETE'. If anybody could help me right now I would really appreciate it. My code is located below.
package password;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class Password {
public void creator() throws IOException {
FileWriter fw = new FileWriter(new File("codes.txt"));
PrintWriter out = new PrintWriter(fw);
char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890".toCharArray();
Random random = new Random();
for (int x = 0; x < 51; x++){
String word = "";
for (int i = 0; i <= 10; i++) {
char c = chars[random.nextInt(chars.length)];
word+=c;
}
out.println(word);
}
fw.close();
}
public void readit() throws FileNotFoundException, InterruptedException {
File file = new File("codes.txt");
Scanner input = new Scanner(file);
//prints each line in the file
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
}
Thread.sleep(10000);
input.close();
}
public void checkit() throws FileNotFoundException, IOException, InterruptedException {
File checkFile = new File("codes.txt");
File tempFile = new File("tempFile.txt");
Scanner input = new Scanner(System.in);
Scanner reader = new Scanner(checkFile);
FileWriter fw = new FileWriter(tempFile);
PrintWriter out = new PrintWriter(fw);
System.out.println("What is the password?");
String word = input.nextLine();
while(reader.hasNextLine()) {
String line = input.nextLine();
if(line.equals(word)){
System.out.println("LOGIN COMPLETE");
Thread.sleep(5000);
} else {
out.println(line);
}
}
reader.close();
fw.close();
checkFile.delete();
tempFile.renameTo(checkFile);
}
}
The main file is below.
package password;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, FileNotFoundException, InterruptedException {
Password pass = new Password();
pass.creator();
pass.readit();
pass.checkit();
}
}
I am a beginner at java so in order for me to understand the code please use simple beginners code.
In the end I've decided there isn't really a need to clear the console screen in Netbeans, and I'll just leave it as is. I do want to give the solution I got in the end for those confused on what I wanted and for anyone who might have the same problems as I did.
package password;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class Password {
//Makes 50 random passwords(ten characters using letters and numbers)
public void creator() throws IOException {
FileWriter fw = new FileWriter(new File("codes.txt"));
PrintWriter out = new PrintWriter(fw);
char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890".toCharArray();
Random random = new Random();
for (int x = 0; x < 51; x++){
String word = "";
for (int i = 0; i <= 10; i++) {
char c = chars[random.nextInt(chars.length)];
word+=c;
}
out.println(word);
}
fw.close();
}
//prints passwords for 10 seconds
public void readit() throws FileNotFoundException, InterruptedException {
File file = new File("codes.txt");
Scanner input = new Scanner(file);
//prints each line in the file
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
}
Thread.sleep(10000);
input.close();
}
//asks for password and if it's correct then states LOGIN COMPLETE and then adds exceptions to a temporary file then readds to main file then closes
public void checkit() throws FileNotFoundException, IOException, InterruptedException {
File file = new File("codes.txt");
FileWriter fw = new FileWriter(new File("code.txt"));
PrintWriter out = new PrintWriter(fw);
Scanner reader = new Scanner(file);
Scanner input = new Scanner(System.in);
System.out.println("Enter a password");
String word = input.nextLine();
//prints each line in the file
while (reader.hasNextLine()) {
String line = reader.nextLine();
if (line.equals(word)) {
System.out.println("LOGIN COMPLETE");
Thread.sleep(5000);
} else {
out.println(line);
}
}
reader.close();
fw.close();
File file2 = new File("code.txt");
Scanner reader2 = new Scanner(file2);
FileWriter fw2 = new FileWriter(new File("codes.txt"));
PrintWriter out2 = new PrintWriter(fw2);
while (reader2.hasNextLine()) {
String line = reader2.nextLine();
out2.println(line);
}
file2.delete();
fw2.close();
System.exit(0);
}
}
Main File Below:
package password;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, FileNotFoundException, InterruptedException {
Password pass = new Password();
pass.creator();
pass.readit();
pass.checkit();
}
}

How to extract ONLY words from a txt file in Java

So I have to extract data from a text file.
The text file is set up like this.
3400 Moderate
310 Light
etc.
I need to extract the numbers, store them in one array, and the strings, and store them in another array so I can do calculations to the numbers based on whats written in the array, and then output that to a file. I've got the last part down, I just cant figure out how to separate the ints from the strings when I extract the data from the txt. file.
Here is what I have now, but it's just extracting the int and the word as a String.
import java.io.*;
import java.util.*;
public class HorseFeed {
public static void main(String[] args){
Scanner sc = null;
try {
sc = new Scanner(new File("C:\\Users\\Patric\\Desktop\\HorseWork.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] arr = lines.toArray(new String[0]);
for(int i = 0; i< 100; i++){
System.out.print(arr[i]);
}
}
}
Use split(String regex) in String class. Set the regex to search for whitespaces OR digits. It will return a String[] which contains words.
If you are analyzing it line by line, you would want another String[] in which you would append all the words from the new lines.
plz, follow the code.
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HorseFeed {
public static void main(String[] args) throws FileNotFoundException, IOException {
List<String> lineList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\Patric\\Desktop\\HorseWork.txt")));
String line;
while ((line = br.readLine()) != null) {
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher(line);
if( pattern.matcher(line).matches()){
while(matcher.find()){
lineList.add(matcher.group());
}
}
}
}
}
here lineList contains your integer.
This should work:
import java.io.*;
import java.util.*;
public class HorseFeed {
public static void main(String[] args) throws FileNotFoundException {
List<Integer> intList = new ArrayList<Integer>();
List<String> strList = new ArrayList<String>();
Scanner sc = new Scanner(new File("C:\\Users\\Patric\\Desktop\\HorseWork.txt"));
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] lineParts = line.split("\\s+");
Integer intValue = Integer.parseInt(lineParts[0]);
String strValue = lineParts[1];
intList.add(intValue);
strList.add(strValue);
}
System.out.println("Values: ");
for(int i = 0; i < intList.size(); i++) {
System.out.print("\t" + intList.get(i) + ": " + strList.get(i));
}
}
}
First extract all text of file and stored it into String . then use replaceall method of string class with pattern to remove digits from it.
Example:
String fileText = new String("welcome 2 java");
ss = fileText.replaceAll("-?\\d+", "");
System.out.println(ss);

How to open and read a file and write specific content to a new file?

First week learning Java. Apologies for my broken code below. Trying to open a file read it and choose needed contents to write in another file. My file looks like this:
DESCRIPTION:
TITILE:
TYPE: image
SOURCE: Library
FORMAT: jpeg
IDENTIFIER: 120034
LATITUDE: 40.109580
LONGITUDE: -88.228378
DESCRIPTION:
TITLE: GSLIS
SUBJECT:
TYPE: image
SOURCE: Library
FORMAT: jpeg
IDENTIFIER: 120155
LATITUDE: 40.107779
LONGITUDE:-88.231621
I just wrote two pieces of code, one for open and read, one for match the patterns:
package Functions;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class readFileLocal {
private static final String[] String = null;
private String path;
public readFileLocal(String file_path){
path = file_path;
}
public String[] openFile() throws IOException{
FileReader freader = new FileReader (path);
BufferedReader textReader = new BufferedReader (freader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i<numberOfLines; i++){
String newLine=new String();
newLine=null;
textData[i] = textReader.readLine();
String a = textData.toString();
while ((textData[i])!=null){
if (a.startsWith("Identifier: ") || a.startsWith("Latitude: ")||a.startsWith("Longitude:")){
newLine.append(a);
}
boolean filled1 =Boolean.valueOf(String newLine[0]);
boolean filled2 =Boolean.valueOf(String newLine[1]);
boolean filled3 =Boolean.valueOf(String newLine[2]);
if(filled1, filled2, filled3) {
System.out.println(newLine[0]+'|'+newLine[1]+"|"+newLine[2]+"\n");
}
}
}
textReader.close();
}
int readLines() throws IOException{
FileReader file_to_read = new FileReader(path);
BufferedReader lines = new BufferedReader (file_to_read);
String aLine=lines.readLine();
int numberOfLines = 0;
while(aLine != null) {
numberOfLines ++;
}
lines.close();
return numberOfLines;
}
}
I also figured how to search a string in the way I wanted with people's help, as shown below:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadLatLong
{
public ReadLatLong(){
String line = "IDENTIFIER: 115956 LATITUDE: 40.104730 LONGITUDE: -88.228798 DATE RIGHTS IDENTIFIER: 115956 LATITUDE: 40.104730 LONGITUDE: -88.228798 DATE RIGHTS";
String pattern = "IDENTIFIER:\\s(\\d*)\\sLATITUDE:\\s(\\d*\\.?\\d*)\\sLONGITUDE:\\s(.*?)\\s";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println("Image: " + m.group(1)+"|"+m.group(2)+"|"+m.group(3));
}
}
}
Now I wonder how to search the whole file to grab all identifier, latitude, longitude, and put them all out like this:
120034 | 40.109580 | -88.228378 \n
120155 | 40.107779 | -88.231621 \n
This is my main method but I don't know how to write it in a new file either.
package readText;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileData {
public static void main(String[] args) throws Exception {
String file_path1 = "F:\\CampusImages.txt";
String file_path2 = "F:\\CampusImageIDs.txt";
try{
ReadFileLocal file = new ReadFileLocal(file_path1);
WriteFile writeout = new WriteFile( file_path2); //Don't know how to write it in new file.
PrintWriter writer = new PrintWriter(new FileWriter(new File(file_path2)));
String[] arylines = file.openFile();
writeout.WriteToFile(String.valueOf(arylines));
int i;
for (i=0; i<arylines.length; i++){
System.out.println(arylines[i]);
}
writer.close();
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
Thanks in advance.
Looks like this is your programming homework, so I won't give you the exact coding. Unless your input file is empty, otherwise your readLines() method will loop forever. if it is not a must, you don't need to count the no. of lines before reading each of them. Here is what you can do:
read one line from the file
while the line is not null
check if the line begins with one of the three identifier you want
if so, append the line to a string
if not, do nothing
if the accumulated string has all three required line, use `ReadLatLong` to do an output
(simplest way is to use 3 bool flags)
read another line using the same variable name as at the beginning
end while
Hope this helps.
EDIT: OK, since RoverMAX has given his coding, I give you another version for you to consider.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestCampusImages {
static final String file_path1 = "CampusImages.txt";
static final String file_path2 = "CampusImageIDs.txt";
static final Pattern pID = Pattern.compile("(?<=IDENTIFIER:)[\\d\\s]*");
static final Pattern pLAT = Pattern.compile("(?<=LATITUDE:)[\\d\\s-.]*");
static final Pattern pLONG = Pattern.compile("(?<=LONGITUDE:)[\\d\\s-.]*");
public static void main(String[] args) {
int id = 0;
float latitude = 0.0f, longitude = 0.0f;
try {
File inFile = new File(file_path1);
BufferedReader textReader = new BufferedReader(new FileReader(inFile));
File outFile = new File(file_path2);
PrintWriter out = new PrintWriter(outFile);
String inLine = textReader.readLine();
while (inLine != null) {
Matcher m = pID.matcher(inLine);
if (m.find()) {
id = Integer.parseInt(m.group().trim());
}
m = pLAT.matcher(inLine);
if (m.find()) {
latitude = Float.parseFloat(m.group().trim());
}
m = pLONG.matcher(inLine);
if (m.find()) {
longitude = Float.parseFloat(m.group().trim());
}
if ((id!=0) && (latitude!=0.0f) && (longitude!=0.0f)) {
out.println(String.format("%d | %f | %f ", id, latitude, longitude));
id = 0;
latitude = 0.0f; longitude = 0.0f;
}//end if
inLine = textReader.readLine();
}//end while
textReader.close();
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}//end try
} //end main
} //end TestCampusImages
This code is good since it allow you to further process the data extract if so required. But it has no validation on the input file. So be careful. If you consider any of the response here is helpful, please mark it as an answer.
I usually do this to read file line by line, and output line by line. I'm sure there's other ways to do this, just experiment around.
try {
FileReader fr = new FileReader(yourfile);
BufferedReader br = new BufferedReader(fr);
FileOutputStream fout = new FileOutputStream (yourOutputFile);
PrintStream ps = new PrintStream(fout);
String line = br.readLine();
while (line != null) {
//Do something;
String writeContent = your_code_to_get_write_content(line);
ps.println(writeContent);
line = br.readLine();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}

Categories