Welcome to Java Examples

Take a cup of tea and Let's Start programming

Showing posts with label FileOutputStream. Show all posts
Showing posts with label FileOutputStream. Show all posts
//notepad import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Scanner; import java.io.*; public class Notepad extends JFrame implements ActionListener { private TextArea textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); private MenuBar menubar = new MenuBar(); private Menu file = new Menu(); private MenuItem openfile = new MenuItem(); private MenuItem savefile = new MenuItem(); private MenuItem close = new MenuItem(); public Notepad(){ this.setSize(500,500); this.setTitle("Java...Notepad Tutorial"); setDefaultCloseOperation(EXIT_ON_CLOSE); this.textarea.setFont(new Font("Century Gothic",Font.BOLD,12)); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(textarea); this.setMenuBar(this.menubar); this.menubar.add(this.file); this.file.setLabel("File"); this.openfile.setLabel("open"); this.openfile.addActionListener(this); this.file.add(this.openfile); this.savefile.setLabel("Save"); this.savefile.addActionListener(this); this.file.add(this.savefile); this.close.setLabel("Close"); this.close.addActionListener(this); this.file.add(close); } public void actionPerformed(ActionEvent e) { if(e.getSource()==close) { this.dispose(); } if(e.getSource()==openfile) { JFileChooser open = new JFileChooser(); int option = open.showOpenDialog(this); if(option == JFileChooser.APPROVE_OPTION) { this.textarea.setText(""); try{ Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath())); while(scan.hasNext()) this.textarea.append(scan.nextLine() +'\n'); } catch(Exception ex) { System.out.println(ex.getMessage()); } } } if(e.getSource()==savefile) { JFileChooser save = new JFileChooser(); int option = save.showSaveDialog(this); if(option == JFileChooser.APPROVE_OPTION) { try{ BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath())); out.write(this.textarea.getText()); out.close(); } catch(Exception ex) { System.out.println(ex.getMessage()); } } } } public static void main(String args[]) { Notepad app = new Notepad(); app.setVisible(true); } }

Output

FileOutputStream  in java


import java.io.*; class fileoutputstrm { public static void main(String[] args) throws Exception { byte data[]="hi... this is a program of fileoutputstream class".getBytes(); //method to write using loop FileOutputStream fileoutputstrm1 = new FileOutputStream("file1.txt"); for(int index_loop=0;index_loop<data.length;index_loop++) { fileoutputstrm1.write(data[index_loop]); } //method to write using write() method FileOutputStream fileoutputstrm2 = new FileOutputStream("file2.txt"); fileoutputstrm2.write(data); //method to write from specific byte to specific point FileOutputStream fileoutputstrm3 = new FileOutputStream("file3.txt"); fileoutputstrm3.write(data,5,15); //close all files fileoutputstrm1.close(); fileoutputstrm2.close(); fileoutputstrm3.close(); } }




















output

it will create 3 .txt file