我对java很新,我不明白我得到的错误。 Eclipse不建议我如何解决它。
我的以下代码出现以下错误:
“令牌上的语法错误”getChannel“,此标记后的标识符”
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class main {
URL website = new URL("http://ts.vtggames.net/extras.zip");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("extras.zip");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
答案 0 :(得分:1)
这是一个声明:
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
它必须进入一个块:基本上,它必须位于包含在{}
中的类中的一些语法结构中。
其他行是变量声明。这些可以走出街区;这就是第一个语法错误在fos.getChannel()
行上的原因。
您可以将所有行放在main
方法中,如下所示:
public class main {
public static void main(String[] args) { // <-- Add this line
URL website = new URL("http://ts.vtggames.net/extras.zip");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("extras.zip");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} // <-- and this line
}
答案 1 :(得分:0)
正如@AndyTurner所提到的,这不是适当的Java代码
/**
* Updates the text of a given element in the properties file.
*
* @param id The id of the element
* @param newText The text that will replace the original text.
*
* @throws IOException If I/O error occurs
*/
public void updateElementText(String id, String newText) throws IOException {
Assertions.checkNotNull(id, "Id must not be null.");
Assertions.checkNotNull(id, "Id must not be an empty string.");
File file = new File(pathName);
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter wr = new BufferedWriter(new FileWriter(file, true));
try {
String line;
while((line = br.readLine()) != null) {
if(line.contains(id)) {
//returns true
System.out.println("Is line there: " + line.contains(id));
//returns helloText
System.out.println("ID: " + extractId(line));
//returns How are you
System.out.println("TEXT: " + extractText(line));
//returns Some new text for a change
System.out.println("NEW_TEXT: " + newText);
// This is where I am trying to replace the old text
// with new text that came from the main method.
line = line.replaceAll(extractText(line), newText);
//wr.newLine();
wr.write(line);
}
}
} catch(IOException e) {
e.printStackTrace();
} finally {
wr.close();
}
}
/**
* Gets the id part of a line that is stored in the
* properties file.
*
* @param element The element the id is got from.
* @return String representation of the id.
*/
private static String extractId(String line) {
final int commaOccurence = getFirstCommaOccurrence(line);
return line.substring(0, commaOccurence);
}
/**
* Gets the text part of a line that is stored in the
* properties file.
*
* @param element The element the text is got from.
* @return String representation of the text.
*/
private static String extractText(String line) {
final int commaOccurence = getFirstCommaOccurrence(line);
return line.substring(commaOccurence + 1, line.length());
}
/**
* Gets the first occurrence of a comma in any given line of a text file.
* @param element
* @return
*/
private static int getFirstCommaOccurrence(String line) {
return line.indexOf(",");
}
来运行独立类