单选题Which gets the name of the parent directory file “file.txt”?()A  String name= File.getParentName(“file.txt”);B  String name= (new File(“file.txt”)).getParent();C  String name = (new File(“file.txt”)).getParentName();D  String name= (new File(“file.txt”

题目
单选题
Which gets the name of the parent directory file “file.txt”?()
A

 String name= File.getParentName(“file.txt”);

B

 String name= (new File(“file.txt”)).getParent();

C

 String name = (new File(“file.txt”)).getParentName();

D

 String name= (new File(“file.txt”)).getParentFile();

E

 Directory dir=(new File (“file.txt”)).getParentDir();  String name= dir.getName();

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

下列InputStream构造方法正确的是()。

A、InputStream in=new FileReader(“file.txt”)

B、InputStream in=new FileInputStream(“file.txt”)

C、InputStream in=new InputStreamFileReader (“file.txt”,“read”)

D、FileInputStream in=new FileReader(new File(“file.txt”))


参考答案:C

第2题:

WhichtwoconstructanOutputSreamthatappendstothefile“file.txt”?()

A.OutputStreamout=newFileOutputStream(“file.txt”);

B.OutputStreamout=newFileOutputStream(“file.txt”,“append”);

C.FileOutputStreamout=newFileOutputStream(“file.txt”,true);

D.FileOutputStreamout=newFileOutputStream(newfile(“file.txt”));

E.OutputStreamout=newFileOutputStream(newFile(“file.txt”)true);


参考答案:C, E

第3题:

【C++代码】

#include <list>

#include <iostream>

#include <string>

using namespace std;

class AbstractFile{

protected:

string name; //文件或目录名称

public:

void printName(){cout<<name;} //打印文件或目录名称

virtual void addChild(AbstractFile *file)=0; //给一个目录增加子目录或文件}

virtual void removeChild(AbstractFile *file)=0; //删除一个目录的子目录或文件

virtual list<AbstractFile*> *getChildren()=0; //获得一个目录的子目录或文件

};

class File:public AbstractFile{

public:

File(string name) {(1) = name;}

void addChild(AbstractFile *file){return;}

void removeChild(AbstractFile *file){return;}

(2) getChildren(){return (3) ;}

};

class Folder :public AbstractFile{

private:

list <AbstractFile*> childList: //存储子目录或文件

public:

Folder(string name){ (4) name;}

void addChild(AbstractFile*file){childList.push_back(file);}

void removeChild(AbstractFile*file){childList.remove(file);}

list<AbstractFile*>*getChildren(){return (5) ;}

};

void main(){

//构造一个树形的文件/目录结构

AbstractFile *rootFolder=new Folder("c:\\ ");

AbstractFile*compositeFolder=new Folder("composite");

AbstractFile *windowsFolder=new Folder("windows");

AbstractFile*file=new File("TestCompositejava");

rootFolder->addChild(compositeFolder);

rootFolder->addChild(windowsFolder);

compositeFolder->addChild(file);


正确答案:
(1)this->name
(2)list<AbstractFile*>*
(3)NULL
(4)this->name
(5)&childList

第4题:

对于如下程序:  #include    main( )  {    FILE *fp;    fp=fopen(“file.txt”,“w”);    fprintf(fp,“%s”,“xyz”);    fclose(fp);  }  若文件file.txt中原有的内容为good,则运行该程序以后,文件file.txt中的内容为()


正确答案:xyz

第5题:

Whichgetsthenameoftheparentdirectoryfile“file.txt”?()

A.Stringname=File.getParentName(“file.txt”);

B.Stringname=(newFile(“file.txt”)).getParent();

C.Stringname=(newFile(“file.txt”)).getParentName();

D.Stringname=(newFile(“file.txt”)).getParentFile();

E.Directorydir=(newFile(“file.txt”)).getParentDir();Stringname=dir.getName();


参考答案:B

第6题:

要想以读方式打开文件“D:\File.txt”,以下不能实现目的的语句是()。

A、ifstreaminf("D:\\FilEtxt",ios::in);

B、ifstreaminf("D:\\File.txt");

C、ofstreaminf("D:\\File.txt",ios::out);

D、fstreaminf("D:\\File.txt",ios::in|ios::out);


参考答案:C

第7题:

Whatwritesthetext“”totheendofthefile“file.txt”?()

A.OutputStreamout=newFileOutputStream(“file.txt”); Out.writeBytes(“/n”);

B.OutputStreamos=newFileOutputStream(“file.txt”,true); DataOutputStreamout=newDataOutputStream(os);out.writeBytes(“/n”);

C.OutputStreamos=newFileOutputStream(“file.txt”); DataOutputStreamout=newDataOutputStream(os);out.writeBytes(“/n”);

D.OutputStreamos=newOutputStream(“file.txt”,true); DataOutputStreamout=newDataOutputStream(os);out.writeBytes(“/n”);


参考答案:B

第8题:

【Java代码】

import Java.util.ArrayList;

import java.util.List;

(1) class AbstractFile{

protected String name;

public void printName(){System.out.println(name);}

public abstract boolean addChild(AbstractFile file);

public abstract boolean removeChild(AbstractF ile file);

public abstract List<AbstractFile> getChildren();

class File extends AbstractFile{

public File(String name){this.name=name;}

public boolean addChild(AbstractFile file){return false;}

public boolean removeChild(AbstractFile file){return false;}

public List<AbstractFile> getChildren(){return (2) ;}

class Folder extends AbstractFile{

private List <AbslractFile> childList;

public Folder(String name){

this.name=name;

this.childList=new ArrayList<AbstractFile>();

public boolean addChild(AbstractFile file) { return childList.add(file);}

public boolean removeChild(AbstractFile file){return childList.remove(file);}

public (3) <AbstractFile> getChildren(){return (4) ;}

public class Client{

public static void main(String[] args){

//构造一个树形的文件/目录结构

AbstractFile rootFolder= new Folder("c:\\ ");

AbstractFile compositeFolder=new Folder("composite");

AbstractFile windowsFolder=new Folder("windows");

AbstractFile file=new File("TestComposite.java");

rootFolder.addChild(compositeFolder) ;

rootFolder.addChild(windowsFolder);

compositeFolder.addChild(file) ;

//打印目录文件树

printTree(rootFolder);

private static void printTree(AbslractFile ifile){

ifile.printName();

List <AbslractFile> children=ifile.getChildreno:

if(children==null) return;

for (AbstractFile file:children) {

(5) ;

}

该程序运行后输出结果为:

c:\

composite

TestComposite.java

Windows


正确答案:
(1)abstract
(2)null
(3)List
(4)childList
(5)printTree(file)

第9题:

阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

【说明】

现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如6—8所示:

【Java代码】

import JavA.util.ArrayList;

import JavA.util.List;(1)class AbstractFile{

protected String name;

public void printName(){System.out.println(name);}

public abstract boolean addChild(AbstractFile file);

public abstract boolean removeChild(AbstractFile file);

public abstract ListgetChildren {};

}

class File extends AbstractFile{

public File(String name)(this.name=name;}

public boolean addChild(AbstractFile file){return false;}

public boolean removeChild(AbstractFile file){return false;}

public ListgetChildren(){return (2) ;)

}

clasS Folder extends AbstractFile{

private ListchildList;

public Folder(String name){

thiS.name=name;

this.childList=new ArrayList{};

}

public boolean addChild(AbstractFile file){return childList.add(file);}

public boolean removeChild(AbstractFile file){return childList.remove(file);

public (3)getChildren(){return (4) ;)

}

public class Client{

public static void main(String[]args){

//构造一个树形的文件/目录结构

AbstractFile rootFolder=new Folder(“C:\”’);

AbstractFile compositeFolder=new Folder(”composite”);

AbstractFile windowsFolder=new Folder(”windows”);

AbstractFile file=new File(”TestComposite.java”);

rootFOlder.addChild (compositeFolder);

rootFolder.addChiid(windowsFolder);

compositeFolder.addChild(file);

//打印目录文件树

printTree(rootFolder);

}

private static void printTree(AbstractFile ifile){

ifile.PrIntName();

Listchildren:ifile.getChildren ();

if(chiidren==null)return;

for(AbstractFile file:children){(5) ;

}

}

}

该程序运行后输出结果为:

C:\

composite

TestComposite.java

Windows


正确答案:(1)Abstract(2)null(3)List(4)childList(5)printTree(file)
(1)Abstract(2)null(3)List(4)childList(5)printTree(file) 解析:Composite模式定义:将对象以树型结构组织起来,以达成“部分-整体”的层次结构,使得客户端对单个对象和组合对象的使用具有一致性。Composite比较容易理解,想到Composite就应该想到树型结构图。组合体内这些对象都有共同接口,当组合体一个对象的方法被调用执行时,Composite将遍历(Iterator)整个树形结构,寻找同样包含这个方法的对象并实现调用执行。AbstractFile为一个抽象文件类,其作用主要是实现对文件或者文件夹的抽象。文件类File继承自AbstractFile。File(stringname)为File类的一个属性,用于获取文件名称。Add-child方法用来给一个目录增加子目录或文件。Removechild方法用于删除一个目录的子目录或文件。Getchildren方法用于获取一个目录或文件,所以返回值类型应该是一个列表形式的AbstractFile,但文件本身不包括子目录,故返回NUIJIJ。Fold类表示一个文件夹,属性Folder用于获取文件夹名称,Getchildren方法返回值应为List型的AbstractFile对象指针。

第10题:

创建一个向文件“file.txt”追加内容的输出流对象的语句有()。

  • A、FileOutputStream out=new FileOutputStream(“file.txt”,true);
  • B、OutputStream out=new FileOutputStream(“file.txt”,“append”);
  • C、OutputStream out=new FileOutputStream(“file.txt”);
  • D、FileOutputStream out=new FileOutputStream(new file(“file.txt”));
  • E、OutputStream out=new FileOutputStream(new File(“file.txt”),true.;

正确答案:A,E

更多相关问题