mnj83

slip 13

Mar 17th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. Q1
  2. import java.io.*;
  3. public class FileDelete
  4. {
  5. public static void main(String args[])throws Exception
  6. {
  7. File f=new File(args[0]);
  8. if(f.isFile())
  9. {
  10. System.out.println(args[0]+" IS FILE");
  11. System.out.println("NAME : "+f.getName()+"\nPATH : "+f.getAbsolutePath()+"\nSIZE: "+f.length()+"\nREADABLE: "+f.canRead()+"\nWritable : "+f.canWrite()+"\nExecutable: "+f.canExecute()+"\n");
  12. }
  13. else if(f.isDirectory())
  14. {
  15. System.out.println(args[0]+" IS DIRECTORY\n");
  16. //-------------------------------------------------------------------------------------------
  17.  
  18. String lst[]=f.list();
  19. /* 1: listing contents of directory
  20. 2: counting how many files and directory. According to it displaying counts
  21. */
  22. int fcnt=0,dcnt=0;
  23. System.out.println("CONTENTS OF DIRECTORY ARE :");
  24. for(int i=0;i<lst.length;i++)
  25. {
  26. System.out.println((i+1)+":"+lst[i]);
  27. File d=new File(f.getPath()+"/"+lst[i]);
  28. if(d.isFile())
  29. fcnt++;
  30. if(d.isDirectory())
  31. dcnt++;
  32. }
  33. System.out.println("NO.OF FILES :"+fcnt);
  34. System.out.println("NO.OF DIRECTORIES:"+dcnt);
  35. //----------------------------------------------------------------------------------------------
  36.  
  37. /*IF it is .txt file,delete it. also print information of all files */
  38.  
  39. System.out.println("\nDISPLAYING INFORMATION OF FILE");
  40. for(int i=0;i<lst.length;i++)
  41. {
  42. File d=new File(f.getPath()+"/"+lst[i]);
  43. if(d.isFile())
  44. {
  45. System.out.println("NAME : "+d.getName()+"\nPATH : "+d.getPath()+"\nSIZE: "+d.length()+"\nREADABLE: "+d.canRead()+"\nWritable : "+d.canWrite()+"\n");
  46. }
  47. }
  48. //---------------------------------------------------------------------------------------------------------------
  49.  
  50.  
  51. /*DELETING .txt FILES */
  52. System.out.println("DELETING .txt FILE\n");
  53. for(int i=0;i<lst.length;i++)
  54. {
  55. if(lst[i].endsWith(".txt"))
  56. {
  57. File d=new File(f.getPath()+"/"+lst[i]);
  58. if(d.isFile())
  59. {
  60. System.out.println("Do you want to delete file : "+lst[i]);
  61. System.out.println("PRESS 1 for YES / 2 for NO");
  62. System.out.print("ENTER YOUR CHOICE : ");
  63. DataInputStream dis=new DataInputStream(System.in);
  64. int choice=Integer.parseInt(dis.readLine());
  65. if(choice==1)
  66. {
  67. d.delete();
  68. System.out.println(lst[i]+" : IS DELETED");
  69. }
  70. }
  71. }
  72. }
  73.  
  74.  
  75. }
  76. else
  77. {
  78. System.out.println(args[0]+" NOT FOUND");
  79. }
  80.  
  81. }
  82. }
  83.  
  84.  
  85. import java.awt.*;
  86. import java.awt.event.*;
  87. import javax.swing.*;
  88. import javax.swing.table.*;
  89. import java.sql.*;
  90. class TableDemo extends JFrame implements ActionListener
  91. {
  92. DefaultTableModel model;
  93. JTable table;
  94. JScrollPane jsp;
  95. JButton btn;
  96. Connection con;
  97. Statement stmt;
  98. ResultSet rs;
  99.  
  100. TableDemo()
  101. {
  102.  
  103. try
  104. {
  105. Class.forName("org.postgresql.Driver");
  106. con=DriverManager.getConnection("jdbc:postgresql://localhost/tydb","root","");
  107. if(con==null)
  108. {
  109. JOptionPane.showMessageDialog(null,"Unable to Connect");
  110. }
  111. }
  112. catch(Exception e)
  113. {
  114.  
  115. JOptionPane.showMessageDialog(null,"Problem in Database connection "+e);
  116. }
  117. setLayout(null);
  118.  
  119. btn=new JButton("SHOW");
  120. btn.addActionListener(this);
  121. btn.setSize(100,30);
  122. btn.setLocation(120,20);
  123. add(btn);
  124.  
  125. model=new DefaultTableModel();
  126. table=new JTable(model);
  127.  
  128. model.addColumn("RNo");
  129. model.addColumn("Name");
  130. model.addColumn("Per");
  131.  
  132. jsp=new JScrollPane(table);
  133. jsp.setSize(300,100);
  134. jsp.setLocation(50,100);
  135. add(jsp);
  136. setTitle("Q.13 Table Demo");
  137. setSize(500,500);
  138. setVisible(true);
  139. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140. }
  141.  
  142. public void actionPerformed(ActionEvent ae)
  143. {
  144. String s=ae.getActionCommand();
  145. if(s.equals("SHOW"))
  146. {
  147. try
  148. {
  149. stmt=con.createStatement();
  150. rs=stmt.executeQuery("select * from student");
  151.  
  152. while(rs.next())
  153. {
  154. String rno=rs.getString(1);
  155. String name=rs.getString(2);
  156. String per=rs.getString(3);
  157. model.addRow(new String[]{rno,name,per});
  158. }
  159. }
  160. catch(Exception e)
  161. {
  162. JOptionPane.showMessageDialog(null,"Problem while fetching result "+e);
  163.  
  164. }
  165. }
  166. }//ActionPerformed
  167.  
  168. public static void main(String s[])
  169. {
  170. new TableDemo();
  171. }//main
  172. }//TableDemo
Add Comment
Please, Sign In to add comment