Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q1
- import java.io.*;
- public class FileDelete
- {
- public static void main(String args[])throws Exception
- {
- File f=new File(args[0]);
- if(f.isFile())
- {
- System.out.println(args[0]+" IS FILE");
- System.out.println("NAME : "+f.getName()+"\nPATH : "+f.getAbsolutePath()+"\nSIZE: "+f.length()+"\nREADABLE: "+f.canRead()+"\nWritable : "+f.canWrite()+"\nExecutable: "+f.canExecute()+"\n");
- }
- else if(f.isDirectory())
- {
- System.out.println(args[0]+" IS DIRECTORY\n");
- //-------------------------------------------------------------------------------------------
- String lst[]=f.list();
- /* 1: listing contents of directory
- 2: counting how many files and directory. According to it displaying counts
- */
- int fcnt=0,dcnt=0;
- System.out.println("CONTENTS OF DIRECTORY ARE :");
- for(int i=0;i<lst.length;i++)
- {
- System.out.println((i+1)+":"+lst[i]);
- File d=new File(f.getPath()+"/"+lst[i]);
- if(d.isFile())
- fcnt++;
- if(d.isDirectory())
- dcnt++;
- }
- System.out.println("NO.OF FILES :"+fcnt);
- System.out.println("NO.OF DIRECTORIES:"+dcnt);
- //----------------------------------------------------------------------------------------------
- /*IF it is .txt file,delete it. also print information of all files */
- System.out.println("\nDISPLAYING INFORMATION OF FILE");
- for(int i=0;i<lst.length;i++)
- {
- File d=new File(f.getPath()+"/"+lst[i]);
- if(d.isFile())
- {
- System.out.println("NAME : "+d.getName()+"\nPATH : "+d.getPath()+"\nSIZE: "+d.length()+"\nREADABLE: "+d.canRead()+"\nWritable : "+d.canWrite()+"\n");
- }
- }
- //---------------------------------------------------------------------------------------------------------------
- /*DELETING .txt FILES */
- System.out.println("DELETING .txt FILE\n");
- for(int i=0;i<lst.length;i++)
- {
- if(lst[i].endsWith(".txt"))
- {
- File d=new File(f.getPath()+"/"+lst[i]);
- if(d.isFile())
- {
- System.out.println("Do you want to delete file : "+lst[i]);
- System.out.println("PRESS 1 for YES / 2 for NO");
- System.out.print("ENTER YOUR CHOICE : ");
- DataInputStream dis=new DataInputStream(System.in);
- int choice=Integer.parseInt(dis.readLine());
- if(choice==1)
- {
- d.delete();
- System.out.println(lst[i]+" : IS DELETED");
- }
- }
- }
- }
- }
- else
- {
- System.out.println(args[0]+" NOT FOUND");
- }
- }
- }
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.table.*;
- import java.sql.*;
- class TableDemo extends JFrame implements ActionListener
- {
- DefaultTableModel model;
- JTable table;
- JScrollPane jsp;
- JButton btn;
- Connection con;
- Statement stmt;
- ResultSet rs;
- TableDemo()
- {
- try
- {
- Class.forName("org.postgresql.Driver");
- con=DriverManager.getConnection("jdbc:postgresql://localhost/tydb","root","");
- if(con==null)
- {
- JOptionPane.showMessageDialog(null,"Unable to Connect");
- }
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null,"Problem in Database connection "+e);
- }
- setLayout(null);
- btn=new JButton("SHOW");
- btn.addActionListener(this);
- btn.setSize(100,30);
- btn.setLocation(120,20);
- add(btn);
- model=new DefaultTableModel();
- table=new JTable(model);
- model.addColumn("RNo");
- model.addColumn("Name");
- model.addColumn("Per");
- jsp=new JScrollPane(table);
- jsp.setSize(300,100);
- jsp.setLocation(50,100);
- add(jsp);
- setTitle("Q.13 Table Demo");
- setSize(500,500);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent ae)
- {
- String s=ae.getActionCommand();
- if(s.equals("SHOW"))
- {
- try
- {
- stmt=con.createStatement();
- rs=stmt.executeQuery("select * from student");
- while(rs.next())
- {
- String rno=rs.getString(1);
- String name=rs.getString(2);
- String per=rs.getString(3);
- model.addRow(new String[]{rno,name,per});
- }
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null,"Problem while fetching result "+e);
- }
- }
- }//ActionPerformed
- public static void main(String s[])
- {
- new TableDemo();
- }//main
- }//TableDemo
Add Comment
Please, Sign In to add comment