1.编写程序,实现从键盘输入字符串并在屏幕上输出。
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
JFrame frm = new JFrame();
JTextArea area=new JTextArea(str,50,50);
frm.setLayout(new FlowLayout());
frm.add(area);
frm.setSize(800,500);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}
2.编写程序,查询文件信息。
import java.io.File;
public class test {
public static void main(String[] args) {
File file1 = new File("C:");
System.out.println(file1.getAbsolutePath());
System.out.println(file1.length());
}
}
3.编写程序,利用Java字符流文件输入/输出类,实现将源程序复制到另一个文件中。
import java.io.*;
public class test {
public static void main(String[] args)throws IOException{
File file=new File("text.txt");
file.createNewFile();
try {
FileInputStream fis=new FileInputStream("D:\\Myproject\\java项目\\test.java");
FileOutputStream fos=new FileOutputStream(file);
int len = fis.available();
byte[] b=new byte[len];
while(fis.read(b)!=-1){
fos.write(b);
}
fis.close();
fos.close();
}catch (IOException e){
e.printStackTrace();
}finally {
}
}
}
4.编写一个程序,比较两个文件的内容是否相同。
import java.io.*;
public class test{
public static void main(String[] args) {
if(isSameFile("D:\\Myproject\\java项目\\test.java","D:\\Myproject\\java项目\\text.txt")){
System.out.println("两个文件完全相同");
}
else{
System.out.println("文件内容不一样");
}
}
public static boolean isSameFile(String fileName1,String fileName2){
FileInputStream fis1 = null;
FileInputStream fis2 = null;
try {
fis1 = new FileInputStream(fileName1);
fis2 = new FileInputStream(fileName2);
int len1 = fis1.available();
int len2 = fis2.available();
if (len1 == len2) {
byte[] data1 = new byte[len1];
byte[] data2 = new byte[len2];
fis1.read(data1);
fis2.read(data2);
for (int i=0; i<len1; i++) {
if (data1[i] != data2[i]) {
return false;
}
}
return true;
} else {
return false;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis1 != null) {
try {
fis1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis2 != null) {
try {
fis2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}
5.编写一个程序,求2~200之间的素数,并将结果保存在文件prime.dat中。再从该文件中读取内容并在屏幕上显示出来。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.*;
public class test {
public static void main(String[] args) {
Judge j=new Judge();
System.out.println(Arrays.toString(j.judge()));
File file=new File("prime.dat");
try{
file.createNewFile();
}
catch(Exception e){
}
try{
Writer fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
fw.write(Arrays.toString(j.judge()));
System.out.println("文件已写入");
fw.close();
}
catch(IOException e){
e.printStackTrace();
}finally {
}
}
}
class Judge{
Integer[] judge(){
List<Integer> list=new ArrayList<Integer>();
for(int i=2;i<=200;i++) {
boolean flag=true;
for(int j=2;j<i;j++) {
if(i%j == 0) {
flag=false;
break;
}
}
if(flag) {
list.add(i);
}
}
Integer[] nsz=new Integer[list.size()];
list.toArray(nsz);
return nsz;
}
}