从输入流中获取文件名 (Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5767175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:40:08  来源:igfitidea点击:

Get filename from an inputstream (Java)

javainputstreamjava-me

提问by Timotheus

if I have this code, how could I keep the filename of the original file or reassign it to the new one?:

如果我有此代码,我如何保留原始文件的文件名或将其重新分配给新文件?:

    InputStream input= assetInfo.openStream();
    File t = new File("");

    OutputStream out = new FileOutputStream(t);

    int read=0;
    byte[] bytes = new byte[1024];

    while((read = input.read(bytes))!= -1){
        out.write(bytes, 0, read);
    }

回答by Jano

An input stream can be created to read from a file or from any other source of data. Therefore it makes no sense to have a filename attached to an input stream. Look in assetInfoto see if that class exposes that data (you can even look inside the class using reflection). Note that the creator or assetInfomade a design mistake not exposing this information, OR you are trying to make one now.

可以创建输入流以从文件或任何其他数据源读取。因此,将文件名附加到输入流是没有意义的。查看assetInfo该类是否公开了该数据(您甚至可以使用反射查看该类的内部)。请注意,创建者或assetInfo犯了一个设计错误,没有公开此信息,或者您现在正在尝试制作一个。