replace a line in a text file by using java - java

How do you replace a line in a text file?
For example you have 1.###
and want to replace it with 1.###
I have program this prgram at the moment.
You search through a list and if you find a string, that you want. You write the string to another file. My porblem is that I don't know how to replace a line in an existing text file.
private static BufferedReader br;
public static void main(String[] args) throws Exception{
try{
FileInputStream fstream = new FileInputStream("C:\\Users\\Timmic\\workspace\\Foutverbeterende codes\\genereren append testbinair apart.txt");
br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
String tokens[] = strLine.split(";");
int x = Integer.parseInt(tokens[2]);
if(x<2){
tokens[3]="###";
String a1 = Arrays.toString(tokens);
String a2 = a1.replaceAll(" ", "");
String a3 = a2.replaceAll(" ", "");
String a6 = a3.replaceAll(",", ";");
String a7 = a6.replaceAll("[<>\\[\\],-]", "");
String a8 = a7 + ";";
System.out.println(a8);
FileWriter fwriter = new FileWriter("d is 2.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);
outputFile.println(a8);
outputFile.close();
}
}
}
catch(Exception e){}
}
and this is the list.
0; 000;0;*;0;0;0;
1; 001;1;*;0;0;1;
2; 010;1;*;0;1;0;
3; 011;2;*;0;1;1;
4; 100;1;*;1;0;0;
5; 101;2;*;1;0;1;
6; 110;2;*;1;1;0;
7; 111;3;*;1;1;1;

// it's okay to throw Exception in main, but ONLY during testing. NEVER EVER
// in production code!
public static void main(String[] args) throws Exception{
FileWriter fwriter = null;
FileInputStream fstream = null;
try {
fstream = new FileInputStream("C:\\Users\\Timmic\\workspace\Foutverbeterende codes\\genereren append testbinair apart.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
// this has to be moved OUT of the loop.
fwriter = new FileWriter("d is 2.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
String tokens[] = strLine.split(";");
int x = Integer.parseInt(tokens[2]);
if(x<2){
tokens[3]="###";
String replaced = Arrays.toString(tokens)
.replaceAll(" ", "");
.replaceAll(" ", "");
.replaceAll(",", ";");
.replaceAll("[<>\\[\\],-]", "");
replaced += ";";
System.out.println(replaced);
outputFile.println(replaced);
}
}
// finally makes sure, that this block is executed, even if something
// goes wrong.
} finally {
if (fstream != null)
fstream.close();
if (fwriter != null)
fwriter.close();
}
}

Related

Parsing data and removing invisible characters like carriage return

I am stuck and for the life of me I can't figure out why I am not getting this data to parse out into 5 tokens.
I load the data below in a file called test.dat:
Row1: 1236~~Tier 5~~54~~updated~~01/05/2019
Row2: 1255934~~Tier 1~~30~~Meeting series continued to review period.~~8/21/2018
Row3: 12556~~Tier 1~~30~~Team began to map out Customer Harm Scenarios in this ~~8/21/2018
Row4: 1255936~~Tier 1~~30~~week's calls.
As of now, calls set through August 8th (~~8/21/2018) end of test file.
See how Row 4 in this editor shows the return carriage? In my file it does not have a visible line break like it does here. It must be hidden characters.
This seems to be causing the problem. I tried to remove it, but nothing seems to work.
Here is my code:
import java.io.*;
import java.util.*;
class MergeData {
public static void main(String[] args) {
System.out.print("Enter master_file:");
String master = loadFile();
HashMap masterMap = readData(master);
}
public static String loadFile(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fileName = new String();
try{
fileName = br.readLine();
System.out.print("opening file... "+ fileName+"\n");
}catch(IOException nfe){
System.err.println("Invalid Format!");
}
return fileName;
}
public static HashMap readData(String file){
HashMap dataMap = new HashMap();
try {
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
String line;
int rowCnt = 0;
while ((line = br.readLine()) != null) {
String clean = line.replaceAll("(?:\\n|\\r)", "");
String[] fields = clean.split("~~");
int size = fields.length;
System.out.println("delimiter size:" + size );
rowCnt = rowCnt +1;
String f1 = null;
String f2 = null;
String f3 = null;
String f4 = null;
String f5 = null;
if(size == 5){
f1 = fields[0];
f2 = fields[1];
f3 = fields[2];
f4 = fields[3];
f5 = fields[4];
} else {
System.out.println("!!! INVALID ROW: id---->" + fields[0]);
}
System.out.println(
"rowCnt:" +rowCnt+ " f1:" +f1+ "----f2:" +f2+
"----f3:" +f3+ "----f4:" + f4+ "----f5:" + f5 +"\n\n");
}
br.close();
fileReader.close();
} catch (IOException e) {
System.out.println("\n File Read Error \n" +e );
}
return dataMap;
}
}

import csv file, Search values and run all value one by one

I have a CSV file where email counts is 550 and I want that I open my website and search this email id and click at save button. For all 550 email process will be same.
Please suggest me how can I will be check one by one..
I use this but it's not working:
public static void main(String[] args) throws IOException
{
try
{
ArrayList<String> ar = new ArrayList<String>();
File csvFile = new File("C:\\Users\\umesh.yadav\\Downloads\\test.xls");
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line = "";
StringTokenizer st = null;
int lineNumber = 0;
int tokenNumber = 0;
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
//for the first line it'll print
System.out.println(arr[0]+" " + arr[1]); // h
// System.out.println("arr[1] = " + arr[1]); // Vito
lineNumber++;
//use comma as token separator
}
}
catch(IOException ex) {
ex.printStackTrace();
}
finally{
//System.out.println("thank you");
}
}

Based on condition txt file divided into two files in java

My target is i have one txt file it contains some line of text. in this i have two words i.e A and 1. if line has "A" letter then next lines goto one file until next line contain "1" and if line contain "1" then next lines goto other file until "A" find.
Input file like follows
A
rahu
pahdu
jhaani
1
hjsdh
dhj
A
jiko
raju
A
tenk
kouou
I am expecting output
A.txt contain
rahu
pahdu
jhaani
Same
1.txt
My code
{
fis = new FileInputStream("E:\\Input.txt");
reader = new BufferedReader(new InputStreamReader(fis));
System.out.println("Reading File line by line using BufferedReader");
String line = reader.readLine();
while(line != null){
if(line.contains("LETTER00~VSAQCCCC~H~")) {
line = reader.readLine();
System.out.println(line);
}
else {
line= reader.readLine();
}
}
}
You could just repoint your FileOutputStream whenever you find a 1 or A.
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("in.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
FileOutputStream fosA = new FileOutputStream("out_A.txt");
FileOutputStream fos1 = new FileOutputStream("out_1.txt");
FileOutputStream fos = null;
System.out.println("Reading File line by line using BufferedReader");
String line = reader.readLine();
while (line != null) {
System.out.println(line);
if(line.equals("A"))
{
fos = fosA;
line = reader.readLine();
continue;
}
if(line.equals("1"))
{
fos = fos1;
line = reader.readLine();
continue;
}
fos.write(line.getBytes());
fos.write('\n');
fos.flush();
line = reader.readLine();
}
fos.close();
fosA.close();
fos1.close();
}
You can do something like this.
System.out.println("Reading File line by line using
BufferedReader");
String inputFIle = "";
String line;
boolean flag = false;
// String line = reader.readLine();
while ((line = reader.readLine()) != null) {
if (line.trim().equalsIgnoreCase("A")) {
inputFIle = "A.txt";
} else if(line.trim().equalsIgnoreCase("1")){
inputFIle = "1.txt";
}
else{
write(line, inputFIle);
}
}
You can do something like this
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new FileReader("Input.txt"));
boolean isFound = false;
List<String> main_list = new ArrayList<>();
List<String> sub_list = new ArrayList<>();
while(scanner.hasNextLine()){
String line = scanner.nextLine();
if(line.contains("A")) {
isFound = true;
} else if(line.contains("1")) {
isFound = false;
for (String aSub_list : sub_list) {
main_list.add(aSub_list);
}
sub_list.clear();
}
if(isFound && !line.contains("A")) {
sub_list.add(line);
}
}
for (String aMain_list : main_list) {
System.out.println(aMain_list);
}
}

How to read from text file and store in String array in Java?

I have a text file textfile.txt which contains data as follows :
A-abc , A-xyz , B-mno , A-ijk , B-pqr
Now, i have to read from this file and store the values in two separate arrays "A" and "B" , such that the values with prefix "A-" gets stored in array A and values with prefix "B-" gets stored in array B.
Also, while storing the data, the prefix needs to be removed i.e. only "abc" needs to be stored in array A.
FileInputStream fstream = new FileInputStream("C:\opt\New_Workspace\Salary.txt");
// use DataInputStream to read binary NOT text
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] arrayLine1= strLine.split(" , ");
for(String s:arrayLine1)
String[] arrayLine2 = s.split(": ");
{
if(s.matches("Basic: "))
{
basic = Double.parseDouble(arrayLine[1]);
}
else if(s.matches("Perc-D ");
{
percD = Double.parseDouble(arrayLine[3]);
}
else if(s.matches("Perc-A: "))
{
percA = Double.parseDouble(arrayLine[5]);
}
}
Try something like this :-
FileInputStream in = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String[] filearray;
filearray = new String[10];
while ((strLine = br.readLine()) != null) {
for (int j = 0; j < myarray.length; j++){
filearray[j] = br.readLine();
}
}
in.close();
This was written in a hurry, so please pardon any errors:
String aStore = "";
String bStore = "";
String aFinal[];
String bFinal[];
try{
Scanner input = new Scanner(new File("file.txt"));
while(input.hasNextLine()){
String message = input.nextLine();
message = message.replace(" ", "");
String store[] = message.split(",");
for(int a = 0; a < store.length; a++){
if((store[a]).contains("A-"){
String t[] = (store[a]).split("-");
aStore = aStore + "_" + t[1];
}
if((store[a]).contains("B-"){
String t[] = (store[a]).split("-");
bStore = bStore + "_" + t[1];
}
}
aFinal = aStore.split("_");
bFinal = bStore.split("-");
}
input.close();
}
catch(Exception e){}

Save content of File as String in Java?

I was working a little bit with config files and file reader classes in java.
I always read/wrote in the files with arrays because I was working with objects.
This looked a little bit like this:
public void loadUserData(ArrayList<User> arraylist) {
try {
List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
for(String line : lines) {
String[] userParams = line.split(";");
String name = userParams[0];
String number= userParams[1];
String mail = userParams[2];
arraylist.add(new User(name, number, mail));
}
} catch (IOException e) {
e.printStackTrace();
}
}
This works fine, but how can I save the content of a file as only one single string?
When I read a file, the string I use should be the exact same as the content of the file (without the use of arrays or line splits).
how can I do that?
Edit:
I try to read a SQL-Statement out of a file to use it with JDBC later on. That's why I need the content of the File as a single String
This method will work
public static void readFromFile() throws Exception{
FileReader fIn = new FileReader("D:\\Test.txt");
BufferedReader br = new BufferedReader(fIn);
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
String text = sb.toString();
System.out.println(text);
}
I hope this is what you need:
public void loadUserData(ArrayList<User> arraylist) {
StringBuilder sb = new StringBuilder();
try {
List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
for(String line : lines) {
// String[] userParams = line.split(";");
//String name = userParams[0];
//String number= userParams[1];
//String mail = userParams[2];
sb.append(line);
}
String jdbcString = sb.toString();
System.out.println("JDBC statements read from file: " + jdbcString );
} catch (IOException e) {
e.printStackTrace();
}
}
or maybe this:
String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);
Just do that:
final FileChannel fc;
final String theFullStuff;
try (
fc = FileChannel.open(path, StandardOpenOptions.READ);
) {
final ByteBuffer buf = ByteBuffer.allocate(fc.size());
fc.read(buf);
theFullStuff = new String(buf.array(), theCharset);
}
nio for the win! :p
You could always create a Buffered reader e.g.
File anInputFile = new File(/*input path*/);
FileReader aFileReader = new FileReader(anInputFile);
BufferedReader reader = new BufferedReader(aFileReader)
String yourSingleString = "";
String aLine = reader.readLine();
while(aLine != null)
{
singleString += aLine + " ";
aLine = reader.readLine();
}

Categories