SlideShare a Scribd company logo
1 of 9
1
// Creating a text File using FileWriter
importjava.io.FileWriter;
importjava.io.IOException;
classCreateFile
{
publicstaticvoidmain(String[] args) throwsIOException
{
// Accepta string
Stringstr = "File HandlinginJavausing"+
" FileWriterandFileReader";
// attach a file toFileWriter
FileWriterfw=newFileWriter("output.txt");
// readcharacter wise fromstringandwrite
// intoFileWriter
for (inti = 0; i < str.length();i++)
fw.write(str.charAt(i));
System.out.println("Writingsuccessful");
//close the file
fw.close();
}
}
2
// Reading data froma file using FileReader
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
classReadFile
{
publicstaticvoidmain(String[] args) throwsIOException
{
// variable declaration
intch;
// checkif File existsornot
FileReaderfr=null;
try
{
fr = newFileReader("text");
}
catch (FileNotFoundExceptionfe)
{
System.out.println("Filenotfound");
}
// readfromFileReadertillthe endof file
while ((ch=fr.read())!=-1)
System.out.print((char)ch);
// close the file
fr.close();
}
}
3
// Java Programto create a text editor using java
importjava.awt.*;
importjavax.swing.*;
importjava.io.*;
importjava.awt.event.*;
importjavax.swing.plaf.metal.*;
importjavax.swing.text.*;
classeditorextendsJFrame implementsActionListener{
// Textcomponent
JTextAreat;
// Frame
JFrame f;
// Constructor
editor()
{
// Create a frame
f = newJFrame("editor");
try {
// Setmetl lookandfeel
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// Settheme toocean
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
}
catch (Exceptione) {
}
4
// Textcomponent
t = newJTextArea();
// Create a menubar
JMenuBarmb = newJMenuBar();
// Create amenuformenu
JMenum1 = newJMenu("File");
// Create menuitems
JMenuItemmi1= newJMenuItem("New");
JMenuItemmi2= newJMenuItem("Open");
JMenuItemmi3= newJMenuItem("Save");
JMenuItemmi9= newJMenuItem("Print");
// Addactionlistener
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi9.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi9);
// Create amenuformenu
JMenum2 = newJMenu("Edit");
5
// Create menuitems
JMenuItemmi4= newJMenuItem("cut");
JMenuItemmi5= newJMenuItem("copy");
JMenuItemmi6= newJMenuItem("paste");
// Addactionlistener
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
m2.add(mi4);
m2.add(mi5);
m2.add(mi6);
JMenuItemmc= newJMenuItem("close");
mc.addActionListener(this);
mb.add(m1);
mb.add(m2);
mb.add(mc);
f.setJMenuBar(mb);
f.add(t);
f.setSize(500,500);
f.show();
}
6
// If a button ispressed
publicvoidactionPerformed(ActionEvente)
{
Strings = e.getActionCommand();
if (s.equals("cut")) {
t.cut();
}
else if (s.equals("copy")) {
t.copy();
}
else if (s.equals("paste")) {
t.paste();
}
else if (s.equals("Save")) {
// Create an objectof JFileChooserclass
JFileChooserj =new JFileChooser("f:");
// Invoke the showsSaveDialogfunctiontoshow the save dialog
intr = j.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
// Setthe label tothe path of the selecteddirectory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// Create a file writer
FileWriterwr= new FileWriter(fi,false);
7
// Create bufferedwritertowrite
BufferedWriterw = new BufferedWriter(wr);
// Write
w.write(t.getText());
w.flush();
w.close();
}
catch (Exceptionevt) {
JOptionPane.showMessageDialog(f,evt.getMessage());
}
}
// If the user cancelledthe operation
else
JOptionPane.showMessageDialog(f,"the usercancelledthe operation");
}
else if (s.equals("Print")) {
try {
// printthe file
t.print();
}
catch (Exceptionevt) {
JOptionPane.showMessageDialog(f,evt.getMessage());
}
}
else if (s.equals("Open")) {
// Create an objectof JFileChooserclass
8
JFileChooserj =new JFileChooser("f:");
// Invoke the showsOpenDialogfunctiontoshow the save dialog
intr = j.showOpenDialog(null);
// If the user selectsafile
if (r == JFileChooser.APPROVE_OPTION) {
// Setthe label tothe path of the selecteddirectory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// String
Strings1 = "", sl = "";
// File reader
FileReaderfr= new FileReader(fi);
// Bufferedreader
BufferedReaderbr= new BufferedReader(fr);
// Initilize sl
sl = br.readLine();
// Take the inputfrom the file
while ((s1= br.readLine())!=null) {
sl = sl + "n" + s1;
}
// Setthe text
9
t.setText(sl);
}
catch (Exceptionevt) {
JOptionPane.showMessageDialog(f,evt.getMessage());
}
}
// If the user cancelledthe operation
else
JOptionPane.showMessageDialog(f,"the usercancelledthe operation");
}
else if (s.equals("New")) {
t.setText("");
}
else if (s.equals("close")) {
f.setVisible(false);
}
}
// Main class
publicstaticvoidmain(Stringargs[])
{
editore = neweditor();
}
}
https://www.geeksforgeeks.org/file-handling-java-using-filewriter-filereader/
https://www.geeksforgeeks.org/different-ways-reading-text-file-java/
https://www.geeksforgeeks.org/java-swing-create-a-simple-text-editor/
https://sites.google.com/site/tyroprogramming/java/text-editor

More Related Content

More from Fajar Baskoro

Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan Appsheet
Fajar Baskoro
 

More from Fajar Baskoro (20)

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptx
 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarter
 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUS
 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptx
 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptx
 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptx
 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi Kaltim
 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolah
 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remaja
 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan Appsheet
 
epl1.pdf
epl1.pdfepl1.pdf
epl1.pdf
 
user.docx
user.docxuser.docx
user.docx
 
Dtmart.pptx
Dtmart.pptxDtmart.pptx
Dtmart.pptx
 
DualTrack-2023.pptx
DualTrack-2023.pptxDualTrack-2023.pptx
DualTrack-2023.pptx
 
BADGE.pptx
BADGE.pptxBADGE.pptx
BADGE.pptx
 
womenatwork.pdf
womenatwork.pdfwomenatwork.pdf
womenatwork.pdf
 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdf
 

Text Editor1

  • 1. 1 // Creating a text File using FileWriter importjava.io.FileWriter; importjava.io.IOException; classCreateFile { publicstaticvoidmain(String[] args) throwsIOException { // Accepta string Stringstr = "File HandlinginJavausing"+ " FileWriterandFileReader"; // attach a file toFileWriter FileWriterfw=newFileWriter("output.txt"); // readcharacter wise fromstringandwrite // intoFileWriter for (inti = 0; i < str.length();i++) fw.write(str.charAt(i)); System.out.println("Writingsuccessful"); //close the file fw.close(); } }
  • 2. 2 // Reading data froma file using FileReader importjava.io.FileNotFoundException; importjava.io.FileReader; importjava.io.IOException; classReadFile { publicstaticvoidmain(String[] args) throwsIOException { // variable declaration intch; // checkif File existsornot FileReaderfr=null; try { fr = newFileReader("text"); } catch (FileNotFoundExceptionfe) { System.out.println("Filenotfound"); } // readfromFileReadertillthe endof file while ((ch=fr.read())!=-1) System.out.print((char)ch); // close the file fr.close(); } }
  • 3. 3 // Java Programto create a text editor using java importjava.awt.*; importjavax.swing.*; importjava.io.*; importjava.awt.event.*; importjavax.swing.plaf.metal.*; importjavax.swing.text.*; classeditorextendsJFrame implementsActionListener{ // Textcomponent JTextAreat; // Frame JFrame f; // Constructor editor() { // Create a frame f = newJFrame("editor"); try { // Setmetl lookandfeel UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); // Settheme toocean MetalLookAndFeel.setCurrentTheme(new OceanTheme()); } catch (Exceptione) { }
  • 4. 4 // Textcomponent t = newJTextArea(); // Create a menubar JMenuBarmb = newJMenuBar(); // Create amenuformenu JMenum1 = newJMenu("File"); // Create menuitems JMenuItemmi1= newJMenuItem("New"); JMenuItemmi2= newJMenuItem("Open"); JMenuItemmi3= newJMenuItem("Save"); JMenuItemmi9= newJMenuItem("Print"); // Addactionlistener mi1.addActionListener(this); mi2.addActionListener(this); mi3.addActionListener(this); mi9.addActionListener(this); m1.add(mi1); m1.add(mi2); m1.add(mi3); m1.add(mi9); // Create amenuformenu JMenum2 = newJMenu("Edit");
  • 5. 5 // Create menuitems JMenuItemmi4= newJMenuItem("cut"); JMenuItemmi5= newJMenuItem("copy"); JMenuItemmi6= newJMenuItem("paste"); // Addactionlistener mi4.addActionListener(this); mi5.addActionListener(this); mi6.addActionListener(this); m2.add(mi4); m2.add(mi5); m2.add(mi6); JMenuItemmc= newJMenuItem("close"); mc.addActionListener(this); mb.add(m1); mb.add(m2); mb.add(mc); f.setJMenuBar(mb); f.add(t); f.setSize(500,500); f.show(); }
  • 6. 6 // If a button ispressed publicvoidactionPerformed(ActionEvente) { Strings = e.getActionCommand(); if (s.equals("cut")) { t.cut(); } else if (s.equals("copy")) { t.copy(); } else if (s.equals("paste")) { t.paste(); } else if (s.equals("Save")) { // Create an objectof JFileChooserclass JFileChooserj =new JFileChooser("f:"); // Invoke the showsSaveDialogfunctiontoshow the save dialog intr = j.showSaveDialog(null); if (r == JFileChooser.APPROVE_OPTION) { // Setthe label tothe path of the selecteddirectory File fi = new File(j.getSelectedFile().getAbsolutePath()); try { // Create a file writer FileWriterwr= new FileWriter(fi,false);
  • 7. 7 // Create bufferedwritertowrite BufferedWriterw = new BufferedWriter(wr); // Write w.write(t.getText()); w.flush(); w.close(); } catch (Exceptionevt) { JOptionPane.showMessageDialog(f,evt.getMessage()); } } // If the user cancelledthe operation else JOptionPane.showMessageDialog(f,"the usercancelledthe operation"); } else if (s.equals("Print")) { try { // printthe file t.print(); } catch (Exceptionevt) { JOptionPane.showMessageDialog(f,evt.getMessage()); } } else if (s.equals("Open")) { // Create an objectof JFileChooserclass
  • 8. 8 JFileChooserj =new JFileChooser("f:"); // Invoke the showsOpenDialogfunctiontoshow the save dialog intr = j.showOpenDialog(null); // If the user selectsafile if (r == JFileChooser.APPROVE_OPTION) { // Setthe label tothe path of the selecteddirectory File fi = new File(j.getSelectedFile().getAbsolutePath()); try { // String Strings1 = "", sl = ""; // File reader FileReaderfr= new FileReader(fi); // Bufferedreader BufferedReaderbr= new BufferedReader(fr); // Initilize sl sl = br.readLine(); // Take the inputfrom the file while ((s1= br.readLine())!=null) { sl = sl + "n" + s1; } // Setthe text
  • 9. 9 t.setText(sl); } catch (Exceptionevt) { JOptionPane.showMessageDialog(f,evt.getMessage()); } } // If the user cancelledthe operation else JOptionPane.showMessageDialog(f,"the usercancelledthe operation"); } else if (s.equals("New")) { t.setText(""); } else if (s.equals("close")) { f.setVisible(false); } } // Main class publicstaticvoidmain(Stringargs[]) { editore = neweditor(); } } https://www.geeksforgeeks.org/file-handling-java-using-filewriter-filereader/ https://www.geeksforgeeks.org/different-ways-reading-text-file-java/ https://www.geeksforgeeks.org/java-swing-create-a-simple-text-editor/ https://sites.google.com/site/tyroprogramming/java/text-editor