谁帮我写一个java 程序:

2025-03-10 12:00:33
推荐回答(3个)
回答1:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package addlinenumbers;

/**
*
* @author Administrator
*
*
*/

/* Import the Scanner class and all classes from java.io */
// Your code here
import java.io.*;
import java.util.Scanner;
public class AddLineNumbers {
public static void main( String [] args )
{
/* Declare variable identifiers to handle text data input from the file and count/increment the linenumber */
// Your code here
FileInputStream in;;Scanner s;
/* Start try block */
// Your code here
try{
/* Instantiate Scanner object for data input from file “dataInput.txt”. There should be no path specification as your input file should be located in your AddLineNumbers project folder */
// Your code here
in=new FileInputStream("dataInput.txt");
s=new Scanner(in);
/* Instantiate a FileOutputStream object for file “dataOutput.txt”. Do not specify a path. Your file will be created in the project folder by NetBeans */
// Your code here
FileOutputStream f=new FileOutputStream("dataOutput.txt");
/* Instantiate a PrintWriter object for writing data to your FileOutputStram object */
// Your code here
PrintWriter w=new PrintWriter (f);

/* Using a while loop control structure, read one line of data from input file, prepend the line number to data read from the file and output concatenated data to output file */
// Your code here
while(s.hasNext())

{
String line= s.nextLine();
w.append(line, 0,line.length());

}
/* Close files and output message indicating the file has been written */
// Your code here
w.close();
s.close();
/* End try block */
}
/* Code FileNotFoundException and IOException catch blocks. Call getMessage(), toString() and printStackTrace() methods on exception object thrown */
// Your code here
catch(FileNotFoundException e)
{e.getMessage();
e.printStackTrace();

}catch(IOException ex)
{

ex.printStackTrace();

}
}
}

回答2:

这个类干什么用?能说下不?

回答3:

public class Test {
private static Test test1=new Test();
private static Test test2=new Test();
private Test(){}
public Test getFirst()
{
return test1;
}
public Test getLast()
{
return test2;
}

}