91 lines
5.7 KiB
HTML
91 lines
5.7 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>removeAllOccurances.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
|
.literal {color: #cc7832}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.ST4 {color: #9876aa}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab7_CalebFontenot/src/main/java/com/calebfontenot/lab7_calebfontenot/removeAllOccurances.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.lab7_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.io.BufferedReader;
|
|
<span class="literal">import</span> java.io.File;
|
|
<span class="literal">import</span> java.io.FileNotFoundException;
|
|
<span class="literal">import</span> java.io.FileReader;
|
|
<span class="literal">import</span> java.io.IOException;
|
|
<span class="literal">import</span> java.io.PrintWriter;
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> removeAllOccurances {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) <span class="literal">throws</span> FileNotFoundException, IOException {
|
|
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter a path to a file: </span><span class="string">"</span>);
|
|
String sourcePath = input.nextLine();
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Replace what with what? (ex. If you want to replace John with Mary, type: </span><span class="literal">\'</span><span class="string">John Mary</span><span class="literal">\'</span><span class="string">): </span><span class="string">"</span>);
|
|
String source = input.next();
|
|
String target = input.next();
|
|
input.nextLine(); <span class="comment">// Consume newline left-over</span>
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter the output file path: </span><span class="string">"</span>);
|
|
String destinationPath = input.nextLine();
|
|
|
|
File sourceFile = <span class="literal">new</span> File(sourcePath);
|
|
File destinationFile = <span class="literal">new</span> File(destinationPath);
|
|
String[] fileContents = <span class="literal">new</span> String[(<span class="literal">int</span>) <span class="ST3">countLines</span>(sourceFile)];
|
|
<span class="literal">try</span> (Scanner sourceScanner = <span class="literal">new</span> Scanner(sourceFile)) {
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="ST3">countLines</span>(sourceFile); ++i) {
|
|
fileContents[i] = sourceScanner.nextLine();
|
|
}
|
|
}
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < fileContents.<span class="ST4">length</span>; ++i) {
|
|
fileContents[i] = fileContents[i].replace(source, target);
|
|
System.<span class="ST2">out</span>.println(fileContents[i]);
|
|
}
|
|
<span class="comment">// Write contents to new file</span>
|
|
<span class="literal">try</span> (PrintWriter fw = <span class="literal">new</span> PrintWriter(destinationFile);) {
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < fileContents.<span class="ST4">length</span>; ++i) {
|
|
fw.println(fileContents[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST1">countLines</span>(File sourceFile) <span class="literal">throws</span> IOException {
|
|
BufferedReader reader = <span class="literal">new</span> BufferedReader(<span class="literal">new</span> FileReader(sourceFile));
|
|
<span class="literal">int</span> lines = <span class="number">0</span>;
|
|
<span class="literal">while</span> (reader.readLine() != <span class="literal">null</span>) {
|
|
lines++;
|
|
}
|
|
reader.close();
|
|
<span class="literal">return</span> lines;
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|