Important alert: (current site time 7/15/2013 3:45:54 PM EDT)
 

VB icon

Console Text editor

Email
Submitted on: 11/1/2004 6:01:01 AM
By: RicardoNZ  
Level: Beginner
User Rating: By 1 Users
Compatibility: Java (JDK 1.4)
Views: 6757
(About the author)
 
     This code allows a text file to be edited in the console/command prompt.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Console Text editor
// Description:This code allows a text file to be edited in the console/command prompt.
// By: RicardoNZ
//
// Inputs:uses System.in and files for input
//
// Side Effects:i would recomend saving all your text files just incase something goes wrong.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4515&lngWId=2//for details.//**************************************

/* ConsoleInput.java
 * Allows txt files to be read, edited and created entirely from the console.
 * Author: Richard s
 */
import java.io.*;
import java.util.*;
public class FileEdit {
 public FileEdit(){
 main(null);
 }
 public static void main(String[] args) {
try {
 // Create object ins and connect it to the console
 InputStreamReader inStream = new InputStreamReader(System.in);
 BufferedReader ins = new BufferedReader(inStream);
 dialog(ins);
 ins.close();
}
catch (IOException ex) {
 System.err.println("i/o error" + ex.getMessage());
 ex.printStackTrace();
}
System.exit(0);
 }
 private static void dialog(BufferedReader i)throws IOException {
String command;
while (true) {
 System.out.print(" Enter command: ");
 command = i.readLine();
 boolean load = false;
 if (command.startsWith("load ")) {
String inputFile = command.substring(command.indexOf(' ') + 1);
System.out.println("\n\n");
print(inputFile);
 }
 
if (command.startsWith("edit")) {
edit(i, command);
 }
 else if (command.startsWith("save")) {
save(i, command);
 }
 else if ("exit".startsWith(command)) {
return;
 }
}
 }
 public static void save(BufferedReader in, String command) {
 
try {
String file;
int space = command.indexOf(' ');
 if(space!= -1)
 file = command.substring(space + 1);
else{
 System.out.println(" Enter File name");
 file = in.readLine();
 }
 FileWriter outF = new FileWriter(file);
 PrintWriter outs = new PrintWriter(outF);
 System.out.println("Enter as many lines as you like and type End to close the file.");
 //read and save data
 String dataLine = in.readLine();
 while (!("End".equals(dataLine))) {
outs.println(dataLine);
dataLine = in.readLine();
	
if(dataLine == null|| dataLine=="")
	dataLine = "\n";
 }
 
 outs.close();
}
catch (IOException ex) {
 System.err.println("i/o error" + ex.getMessage());
 ex.printStackTrace();
}
 }
 public static void edit(BufferedReader in, String command) {
 
 
 
 Vector vFile = new Vector();
 Vector section;
 try {
String file;
int space = command.indexOf(' ');
 if(space!= -1)
 file = command.substring(space + 1);
else{
 System.out.println(" Enter File name");
 file = in.readLine();
 }
 
 FileReader inS = new FileReader(file);
 BufferedReader Ins = new BufferedReader(inS);
 //read and print data
 
 String dataLine = Ins.readLine();
 //vFile.add(dataLine);
 int lines = 1;
 while (dataLine != null) {
System.out.println(lines + " | " + dataLine);
if(!vFile.add(dataLine))
	 break;
	 
	dataLine = Ins.readLine();
	 
lines++;
 }
 
Ins.close();
 while(true){
System.out.print("Enter line to edit or press 's' to add a section of text at any point: ");
 command = in.readLine();
 if("exit".startsWith(command))
 break;
 else if(command.charAt(0)=='a'){
 System.out.println("Until i fix this - just put a new line character in the file... (>n)");
 }
 else if (command.charAt(0)=='s'){
 System.out.println("Enter lines you want added to the file (type End to stop)");
 
 section = new Vector();
 
 String toAdd = in.readLine();
 while (!("End".equals(toAdd))) {
section.add(toAdd);
toAdd = in.readLine();
	
if(toAdd == null|| dataLine=="")
	toAdd = "\n";
 }
 
System.out.print("Enter line where you wish to add section (or cancel): ");
 command = in.readLine();
 int line = Integer.parseInt(command.trim());
 
 for(int i = section.size(); i>0; i--){
 
 vFile.insertElementAt(section.remove(section.size()-1), line);
 }
 
 }
 else{
int line = Integer.parseInt(command.trim());
	
	System.out.print("Enter replacement for line: \n");
	System.out.print(vFile.get(line-1));
	command = in.readLine();
	if(command.charAt(0)=='>'){
	String add = "";
	for(int i = 0; i<Integer.parseInt(command.substring(1)); i++){
	add += "\n";
	vFile.insertElementAt(add, line-1);
	}
	}
	else{
	
	vFile.setElementAt(command, line-1);
	}
	
	}
	}
	
	FileWriter outF = new FileWriter(file);
	 PrintWriter outs = new PrintWriter(outF);
	 int size = vFile.size();
for(int i = 0; i< size; i++){
String s = (String)vFile.remove(0);
 
 
outs.println(s);
}
outs.close();
}
catch (IOException ex) {
 System.err.println("i/o error" + ex.getMessage());
 ex.printStackTrace();
}
 }
 
 public static void print(String file) {
try {
 FileReader inS = new FileReader(file);
 BufferedReader Ins = new BufferedReader(inS);
 //read and print data
 String dataLine = Ins.readLine();
 while (dataLine != null) {
System.out.println(dataLine);
dataLine = Ins.readLine();
 }
 Ins.close();
}
catch (IOException ex) {
 System.err.println("i/o error" + ex.getMessage());
 ex.printStackTrace();
}
 }
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
11/1/2004 6:37:22 AMRicardoNZ

Feed back would be much appreciated - if you tried the program can you please leave a comment =P
(If this comment was disrespectful, please report it.)

 
11/4/2004 8:05:16 AMAswin Anand

Hi!! the code is nice. Can u make it more interactive, like vi in linux? 4 globes from me
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.