如果你有一个java.io.InputStream对象,你应该如何处理该对象并产生一个String?
假设我有一个InputStream包含文本数据,并且我想将其转换为a String,所以例如我可以将其写入日志文件.
采取InputStream并将其转换为最简单的方法是String什么?
public String convertStreamToString(InputStream is) {
// ???
}
Run Code Online (Sandbox Code Playgroud) 我想使用访问我当前的工作目录
String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);
Run Code Online (Sandbox Code Playgroud)
输出:
Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32
Run Code Online (Sandbox Code Playgroud)
我的输出不正确,因为C盘不是我当前的目录.在这方面需要帮助.
线程的上下文类加载器和普通的类加载器有什么区别?
也就是说,如果Thread.currentThread().getContextClassLoader()并getClass().getClassLoader()返回不同的类加载器对象,将使用哪一个?
我有一个方法,希望其中一个输入变量是java.io.File类型,但我得到的只是InputStream.另外,我无法更改方法的签名.
如何将InputStream转换为File类型,而不实际将文件写入文件系统?
我有一个在我的类路径中的文件,例如com/path/to/file.txt.我需要加载或引用此文件作为java.io.File对象.这是因为我需要使用java.io.RandomAccessFile(文件很大,我需要寻找某个字节偏移量)来访问该文件.这可能吗?构造函数RandomAccessFile需要File实例或String(路径).
如果有另一个解决方案寻求某个字节偏移并读取该行,我也对此持开放态度.
我有几个小时的问题,我尝试了我在教程中找到的所有解决方案.这很简单:我无法访问资源文件.我尝试打开我已经把一个文件src/main/resources和src/test/resources.
我有一个简单的Java项目,我使用带有Eclipse作为IDE的Maven和m2e插件.我想使用Maven的资源过滤,具有不同的配置文件,并且我的POM.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<debug>false</debug>
<optimize>true</optimize>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<filters>
<filter>src/main/filters/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>LOCAL</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>local</env>
</properties>
</profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)
我在src/java/test中做了一个简单的测试:
@Test
public void testOpenResourceFile() {
File testf=new File("/test.txt");
assertTrue(testf.exists());
}
Run Code Online (Sandbox Code Playgroud)
所以,在Eclipse中,我运行(在我的项目文件夹中,在包视图中):
使用env:LOCAL …
我想在java中构建一个可运行的jar.我需要在jar中包含一些文件,这样当我执行jar时,文件会自动从java类中读取.因此,我在项目中创建了一个文件夹,并从项目中引用了这些文件.我在一些教程后创建了jar文件,但是我无法将这些外部文件包含在我的jar文件中.请告诉我有关使用外部文件创建可运行jar的信息.
我的文件结构是
Test
|
|
-------src
| |
| default package
| |
| |
| test1.java
|
-------FileFOlder
| |
| |
| abc.txt
Run Code Online (Sandbox Code Playgroud)
我访问的abc.txt在test1.java类.我的代码是,
public class test1 {
public static void main(String[] args) throws IOException {
char [] read = new char[20];
String path = new File(".").getCanonicalPath();
path = path+"\\Newfolder\\abc.txt";
System.out.println(path);
File nF = new File(path);
FileReader fR = new FileReader(nF);
fR.read(read);
for(char c : read){
System.out.print(c);
}
fR.close();
System.out.println(" Hi..This is test program …Run Code Online (Sandbox Code Playgroud) 如果我使用Java在Eclipse中构建应用程序,并且我希望此应用程序从txt文件中读取,我应该将txt文件放在包中,以便不必指定其路径(这样我就可以说" file.txt的").我认为它应该进入src文件夹,但它不起作用.
我的项目结构如下所示.我不希望将配置文件包含为资源,而是在运行时读取它,以便我们可以简单地更改设置而无需重新编译和部署.我的问题是两件事
我的项目结构:
myproj\
\src
\main
\config
\com\my_app1\
config.dev.properties
config.qa.properties
\java
\com\myapp1\
\model\
\service\
\util\
Config.java
\test
Run Code Online (Sandbox Code Playgroud)
Config.java:
public Config(){
try {
String configPath = "/config.dev.properties"; //TODO: pass env in as parameter
System.out.println(configPath);
final File configFile = new File(configPath);
FileInputStream input = new FileInputStream(configFile);
Properties prop = new Properties()
prop.load(input);
String prop1 = prop.getProperty("PROP1");
System.out.println(prop1);
} catch (IOException ex) {
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 我的播放应用程序是2.4.2.在开发人员模式中,我曾经使用Source.fromFile("./ public/files/abc.json")从后端控制器的公用文件夹中读取文件
当我将应用程序转换为生产模式时,我收到了File Not Found Exceptions.我发现公用文件夹在生产模式下打包在资产jar中.我能做什么才能在开发和生产模式下工作?