我正在构建一个"反向控制台"(以便写入的行将自己添加到顶部而不是底部),因为我偶然发现了Console.MoveBufferArea方法的一个非常奇怪的行为:
static void Main()
{
for (var _linesWritten = 0; _linesWritten < 1000; _linesWritten++)
{
var _height = Math.Min(Console.BufferHeight-1, _linesWritten);
Console.MoveBufferArea(0, 0, Console.BufferWidth, _height, 0, 1);
Console.SetCursorPosition(0, 0);
Console.WriteLine("Line {0} aaaaaaaaaa", _linesWritten);
Console.ResetColor();
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用它固定次数时,它会抛出一个System.IO.IOException:"没有足够的存储空间来处理这个命令".我发现它取决于移动的缓冲区的数量.抛出异常之前写入的行数随着我更改Console.BufferWidth属性而更改.
我正在运行Windows 7 x64 @ Corei7,6gb DDR3,所以存储不是问题....有没有人知道什么可能是错的?
我有以下代码:
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
代码取自java.sun.com.
关于上面给出的短代码,我有几个问题.
为什么我们要捕获IOException.我虽然IOException是在我们使用输入输出方法(而不是网络方法)时可能发生的事情.
"System.err.println"和"System.println"有什么区别?
在catch语句中,我们有"e".做什么的?我们后者使用它吗?
我对使用Java进行基本文件读取非常困惑.很多混合信号.
我尝试了几种不同的方式,每次都会遇到一个未找到的异常.我已经检查了一个文件对象设置为当前路径以打印当前目录,我确实在我正在尝试打开的文件所在的目录中.权限已设置,以便每个人都可以阅读.我不确定发生了什么:
BufferedReader infixLines = new BufferedReader ( new FileReader ( "input.infix" ));
Run Code Online (Sandbox Code Playgroud)
这是抛出错误的行,因此使用infixLines的每个连续行也会抛出错误.
我也尝试使用FileInputStream并得到同样的错误.
这就是说干脆
File file = new File("input.infix");
if ( file.exists() )
System.out.println( "Exists" );
Run Code Online (Sandbox Code Playgroud)
做的工作.
非常困惑.
编辑:( Stacktrace?)
ParseInfix.java:13: unreported exception java.io.FileNotFoundException; must be
BufferedReader infixLines = new BufferedReader(new FileReader (n
^
ParseInfix.java:15: unreported exception java.io.IOException; must be caught or
while ( ( line = infixLines.readLine()) != null )
Run Code Online (Sandbox Code Playgroud) 我想在我的java程序中打开一个文件,但我无法让它工作.我希望我的程序可以在所有平台上运行,尤其是在ubuntu中.
这是我有的:
private void openFileChoser() {
JFileChooser chooser = new JFileChooser(".");
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
openFile(chooser.getSelectedFile().getAbsolutePath());
} else {
System.out.println("Aucun fichier choisi");
}
}
/***
* ouverture du fichier :
* @param name
*/
private void openFile(String name) {
System.out.println("Ouverture du fichier: " +
name);
try {
Runtime.getRuntime().exec(new String[]{name});
//Desktop.getDesktop().open(new File(name)); //doesn't work on ubuntu 13.04
} catch (Exception e) {
System.err.println("Erreur d'ouverture du fichier : "+name);
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我试图打开文件:/home/user/IdeaProjects/androidresourcehelper/test.xls
我得到了例外:
java.io.IOException: Cannot run program …Run Code Online (Sandbox Code Playgroud) 我无法在下面的代码中解决以下异常.我使用BufferedReader的方式有什么问题?我在main方法中使用BufferedReader
输出: -
ParseFileName.java:56: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
Run Code Online (Sandbox Code Playgroud)
// ParseFileName is used to get the file name from a file path
// For eg: get - crc.v from "$ROOT/rtl/..path/crc.v"
import java.util.regex.Pattern;
import java.io.*;
public class ParseFileName {
//Split along /'s , and collect the last term.
public String getName (String longName) {
String splitAt = "/";
Pattern pattern1 = Pattern.compile(splitAt);
String[] parts = pattern1.split(longName);
System.out.println("\nparts.length = …Run Code Online (Sandbox Code Playgroud) 在类下面运行下面的代码 FlightSearch
String moreSearch = "y";
List<Flight> resultList;
// load initial flight data into DB
if (!init()) {
return;
}
// A background thread to monitor changes in csv repository
FileListner fl = new FileListner();
fl.start();
do {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// main thread gets input
Input inputQuery = new Input();
try {
inputQuery.getQuery();
} catch (InvalidException e1) {
e1.getMessage();
} finally {
fl.stopThread();
}
// main thread STARTs processing task as background …Run Code Online (Sandbox Code Playgroud) 我正在整合我的android项目flickr,为此我从这个链接下载了示例代码.当我尝试从IOException我在其中显示的库中上传图像时Logcat.
java.io.IOException: Connection Failed. Response Code: 403, Response Message: Forbidden, Error: <?xml version="1.0" encoding="utf-8" ?><rsp stat="fail"> <err code="95" msg="SSL is required" /></rsp>
Run Code Online (Sandbox Code Playgroud) 我java.io.IOException在编译时遇到了未处理的异常.我已经发布了下面的代码,并指出了错误行.我搜索了许多关于这个问题的帖子,但我没有得到任何相关的解决方案.
MainActivity.java:
public class MainActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
...................
}
public void startRecording() throws IOException {
..................
}
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (RecordFile != null && ifexist.equals("true")) {
}
else {
if (isRecclicked == true) {
recordButtonPressed = true;
try {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after 2 seconds
startRecording(); ----> Unhandled Exception java.io.iOException …Run Code Online (Sandbox Code Playgroud) 我正在尝试读取已保存在我的目录中的文本文件,并将其作为TextView打印在屏幕上.这是我到目前为止的代码.但是,当我运行应用程序时,它会创建一个"错误读取文件"的Toast.我在这做错了什么?
public class sub extends Activity {
private TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
//text = (TextView) findViewById(R.id.summtext);
//File file = new File("inputNews.txt");
//StringBuilder text = new StringBuilder();
try {
InputStream in = openFileInput("inputNews.txt");
if(in != null){
InputStreamReader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);
StringBuilder text = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
in.close();
}
}
catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
TextView …Run Code Online (Sandbox Code Playgroud) 如何(如果有的话)从中获取HTTP状态代码java.io.IOException in java
?