简单的java程序设计题目
1.制作一个招聘广告。希望为不同的企业发布不同的招聘信息,要求用继承,接口与多态测试程序2.已知一个文件"myfile1.txt"中的内容为:student,要求将其写入...
1.制作一个招聘广告。希望为不同的企业发布不同的招聘信息,要求用继承,接口与多态测试程序
2.已知一个文件"myfile1.txt"中的内容为:student,要求将其写入另一个文件中并将字符转换为大写:SUTDENT. 展开
2.已知一个文件"myfile1.txt"中的内容为:student,要求将其写入另一个文件中并将字符转换为大写:SUTDENT. 展开
3个回答
展开全部
第一题:
public class Test {// 测试类
public static void main(String[] args) {
List<Company> companies=new ArrayList<Company>();
companies.add(new CompanyA());
companies.add(new CompanyB());
for (Company company : companies) {
company.brand();
}
}
}
class Company{
public void brand(){
System.out.println("广告");
}
}
class CompanyA extends Company{
public void brand(){
System.out.println("广告A");
}
}
class CompanyB extends Company{
public void brand(){
System.out.println("广告B");
}
}
第二题:
public static void main (String[] args) throws Exception{
String read= readFileContent("d:/myfile1.txt","utf-8");
newFile("d:","myfile2.txt",read);
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @param String
* encoding 文本文件打开的编码方式 例如 GBK,UTF-8
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName,
String encoding) throws IOException {
if (filePathAndName == null || filePathAndName.equals("")) {
return "";
}
if (!new File(filePathAndName).exists()) {
return "";
}
StringBuffer str = new StringBuffer("");
FileInputStream fs = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fs = new FileInputStream(filePathAndName);
if (encoding == null || encoding.trim().equals("")) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding.trim());
}
br = new BufferedReader(isr);
String data = "";
while ((data = br.readLine()) != null) {
str.append(data.toUpperCase());
}
} catch (IOException e) {
throw e;
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (fs != null)
fs.close();
} catch (IOException e) {
throw e;
}
}
return str.toString();
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param String
* content 内容
* @throws IOException
*/
public static void newFile(String path, String fileName, String content) throws IOException {
FileWriter fw = null;
BufferedWriter bw = null;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
try {
fw = new FileWriter(path + File.separator + fileName);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
System.out.println("写入文件出错");
throw e;
} finally {
if (bw != null) {
bw.flush();
bw.close();
}
if (fw != null)
fw.close();
}
}
}
public class Test {// 测试类
public static void main(String[] args) {
List<Company> companies=new ArrayList<Company>();
companies.add(new CompanyA());
companies.add(new CompanyB());
for (Company company : companies) {
company.brand();
}
}
}
class Company{
public void brand(){
System.out.println("广告");
}
}
class CompanyA extends Company{
public void brand(){
System.out.println("广告A");
}
}
class CompanyB extends Company{
public void brand(){
System.out.println("广告B");
}
}
第二题:
public static void main (String[] args) throws Exception{
String read= readFileContent("d:/myfile1.txt","utf-8");
newFile("d:","myfile2.txt",read);
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @param String
* encoding 文本文件打开的编码方式 例如 GBK,UTF-8
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName,
String encoding) throws IOException {
if (filePathAndName == null || filePathAndName.equals("")) {
return "";
}
if (!new File(filePathAndName).exists()) {
return "";
}
StringBuffer str = new StringBuffer("");
FileInputStream fs = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fs = new FileInputStream(filePathAndName);
if (encoding == null || encoding.trim().equals("")) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding.trim());
}
br = new BufferedReader(isr);
String data = "";
while ((data = br.readLine()) != null) {
str.append(data.toUpperCase());
}
} catch (IOException e) {
throw e;
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (fs != null)
fs.close();
} catch (IOException e) {
throw e;
}
}
return str.toString();
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param String
* content 内容
* @throws IOException
*/
public static void newFile(String path, String fileName, String content) throws IOException {
FileWriter fw = null;
BufferedWriter bw = null;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
try {
fw = new FileWriter(path + File.separator + fileName);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
System.out.println("写入文件出错");
throw e;
} finally {
if (bw != null) {
bw.flush();
bw.close();
}
if (fw != null)
fw.close();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询