我想用这段代码写一些东西但是在我运行之后,字符之间有空格.但在代码中我不给字符串空间.
import java.io.*;
public class WriteText{
public static void main(String[] args) {
FileOutputStream fos;
DataOutputStream dos;
try {
File file= new File("C:\\JavaWorks\\gui\\bin\\hakki\\out.txt");
fos = new FileOutputStream(file);
dos=new DataOutputStream(fos);
dos.writeChars("Hello World!");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是(在文本文件中): H e l l o W o r l d !
我试图将值(在文本区域中键入)存储到txt文件中.以下代码正在创建program.txt文件,但数据未写入其中.我究竟做错了什么?
另外,如果我想在用户指定的.txt文件中保存这些值,怎么办呢?
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
o = new BufferedWriter(new FileWriter("program.txt"));
o.write(t1.getText());
o.write(",");
o.write(t2.getText());
o.write(",");
o.write(t3.getText());
o.write(",");
o.write(t4.getText());
o.write(",");
o.write(t5.getText());
o.write(",");
o.write(t6.getText());
o.write(",");
o.write(t7.getText());
o.write(",");
}
catch (IOException e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我试图让一段代码工作.目的是检查某个目录中是否存在.XML文件.
这是我到目前为止所得到的.
File f = new File("saves/*.xml");
if(f.exists()) {
/* Do Something */
} else {
/* do something else */
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用通配符来搜索以.XML结尾的任何文件,我在这里错过了一些简单的东西吗?有没有更简单的方法来检查指定目录中是否存在至少一个.XML文件?
提前致谢.
我有这个代码:
Socket incomingConnection = serverSocket.accept();
String strategy = "1";
Client client = new Client(incomingConnection, this, strategy);
Run Code Online (Sandbox Code Playgroud)
客户构造函数:
public Client(Socket socket, ChatServer chatServer, String strategy) throws IOException{
this.socket = socket;
this.inputStream = socket.getInputStream();
this.outputStream = socket.getOutputStream();
this.chatServer = chatServer;
this.instance1 = new Strategy1(chatServer, this);
this.instance2 = new Strategy2(chatServer, this);
this.strategy = (this.instance1.getName().equals(strategy1) ? this.instance1 : this.instance2);
this.strategy.setStreams();
}
Run Code Online (Sandbox Code Playgroud)
现在看起来像Strategy1:
public class Strategy1{
public Strategy1(ChatServer server, Client client) throws IOException{
this.chatServer = server;
this.client = client;
}
public void …Run Code Online (Sandbox Code Playgroud) 我正在阅读一个文件;
FileReader reader = new FileReader(source);
FileWriter writer = new FileWriter((destination));
int char = 0;
while ((char = reader.read()) != -1) {
writer.write(char);
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想首先检查源文件是否包含某一行,然后是换行符,如果是,我想跳过这些行写入目标文件.
请注意,我想跳过的换行符将被添加到源文件中;
System.getProperty("line.separator").getBytes()
Run Code Online (Sandbox Code Playgroud)
非常感谢!
我想在文件中将字符串/字符数据写为字节.我希望这种转换在IO.*类内部发生.我不想在字符串上使用getBytes()方法.
我尝试了两个程序,但都将数据写为字符.当我在记事本中打开文件时,我可以阅读这些字符.如何将数据存储为字节?
import IO.FileWrite;
import java.io.*;
public class CharToChar {
private final String data;
public CharToChar(String data){
this.data = data;
}
public static void main(String[] args) throws IOException {
final CharToChar charToChar = new CharToChar("I am Manish");
charToChar.write();
}
private void write() throws IOException {
final File file = new File("CharToChar.txt");
final FileWriter fileWriter = new FileWriter(file);
final BufferedWriter bufferdWriter = new BufferedWriter(fileWriter);
bufferdWriter.write(this.data);
bufferdWriter.close();
}
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteStringAsBytesToFile {
public static void …Run Code Online (Sandbox Code Playgroud) 我想将输入流转换为字节数组.我知道我可以使用来自commons-io的IOUtils.但我正在练习java io的一些基础知识.我使用BufferedReader读取了一个xml文件,并尝试使用BufferedWriter将其写入ByteArrayOutputStream.但它不起作用.
当我直接写入ByteArrayOutputStream时,它正在工作.我的代码有什么问题?
try (InputStream inputStream = getClass().getResourceAsStream(
"/productInventory.xml");
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(arrayOutputStream));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));) {
String line = "";
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
}
System.out.println(arrayOutputStream.size());
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
当我在循环中包含下面的行时它的工作
arrayOutputStream.write(line.getBytes(), 0, line.getBytes().length);
Run Code Online (Sandbox Code Playgroud)
使用BufferedWriter时出了什么问题?
我现在正在寻找几个小时,但我找不到我想要的东西.我知道GWT客户端软件包不支持java.io.File.我想在服务器包中使用它.当我编译应用程序时,我收到以下错误:
Compiling module com.xxx.yyy.GWTApp
Validating units:
[ERROR] Errors in 'file:/.../workspace/GWTApp/src/com/xxx/yyy/server/Class1.java'
[ERROR] Line 26: No source code is available for type java.io.File; did you forget to inherit a required module?
[ERROR] Errors in 'file:/.../workspace/GWTApp/src/com/xxx/yyy/server/Class2.java'
[ERROR] Line 104: No source code is available for type java.io.BufferedReader; did you forget to inherit a required module?
[ERROR] Line 104: No source code is available for type java.io.FileReader; did you forget to inherit a required module?
...
Run Code Online (Sandbox Code Playgroud)
任何想法可能是错的?
编辑*.gwt.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC …Run Code Online (Sandbox Code Playgroud) 我有一个简单的文本文件,由于某种原因,它无法找到.我没有看到代码有什么问题,因为我是从网站上得到的,我开始认为我没有将文本文件放在正确的位置.有什么建议吗?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.nio.file.Path;
import java.nio.file.Paths;
public class MainFavorites {
public static void main(String[] args) throws IOException {
/**
* finds pathway to the file
*/
// File file = new File("icecreamTopping.txt");
// System.out.println(file.getCanonicalPath());
BufferedReader reader = null;
ArrayList <String> myFileLines = new ArrayList <String>();
try {
String sCurrentLine;
reader = new BufferedReader(new FileReader("icecreamTopping.txt"));
while ((sCurrentLine = reader.readLine()) != null) {
//System.out.println(sCurrentLine);
myFileLines.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我想阅读并打印文本文件到控制台,所以我用下面的代码做了这个
File file = new File("G:\\text.txt");
FileReader fileReader = new FileReader(file);
int ascii = fileReader.read();
while (ascii != -1)
{
result = result + (char) ascii;
ascii = fileReader.read();
}
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
虽然我得到了正确的结果,但在某些情况下我会得到一些奇怪的结果.假设我的文本文件中包含此文本:
Hello to every one
Run Code Online (Sandbox Code Playgroud)
为了得到一个文本文件我用过记事本,当我改变编码模式时,我的代码会得到奇怪的输出.
安西:大家好
Unicode:ÿþhellotoeveryone
Unicode大端:þÿhellotoeveryone
UTF-8:你好
为什么我会得到这些奇怪的输出?我的代码有问题吗?或者还有其他原因